> Here's the code that I'd like to make flexible to handle
> all the input id's and error message id's.  How could I write it?

Here's one way to do it:

$('#street_number, #street_name, #city').blur(function(){
  $(this.id.replace(/_/g, '-')+'-error')
      [this.value.length? 'fadeOut' : 'fadeIn'](500);
});
$('#street_number').blur(function(){
  $('#submit').attr('disabled', this.value.length? '' : 'disabled');
});

If you could standardize on either underscores or dashes for class/id
values it would save the trouble of doing string replaces to build the
ids.

Instead of naming all the fields, it might be better to tag them all
with a class like "nonempty" or "required" and then select them that
way. Or conversely, tag the optional fields with an "optional" class
and select all the non-optional elements with something like "input:not
(.optional)".

Reply via email to