Rick - one shortcut you can do in your selector is:

$('input:text').each(function...

That might get you a better response from the DOM.  I think the problem you
are seeing might be because of your single quotes around text:
$('input[type=text]') not $("inp...@type='text']").

-- Josh


-----Original Message-----
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Rick Faircloth
Sent: Monday, December 22, 2008 10:10 AM
To: jquery-en@googlegroups.com
Subject: [jQuery] How can I generalize this code for all values?


Don't know if that's the best phrasing for the subject,
but what I'm trying to do is develop some code that
will work for all for inputs of type 'text', instead
of hard-coding the id values.

The original code is this:

$('input#street_number').blur(function() {
                        
     if (this.value.length == 0)
     { $('#street-number-required-error').fadeIn(500);
       $('#submit').attr('disabled', 'disabled') }
     if (this.value.length > 0)
     { $('#street-number-required-error').fadeOut(500);
       $('#submit').attr('disabled', '') };
});
                    
$('input#street_name').blur(function() {
                    
     if (this.value.length == 0)
     { $('#street-name-required-error').fadeIn(500);
       $('#submit').attr('disabled', 'disabled') }
     if (this.value.length > 0)
     { $('#street-name-required-error').fadeOut(500);
       $('#submit').attr('disabled', '') };
});


Here's my coding attempt: (no errors in firebug, but not response
from the DOM)...

$(document).ready(function() {
     $("inp...@type='text']").each(function(i) {
          $(this).blur(function() {
               if (this.value.length == 0)
               { $(this.id.replace(/_/g, '-')+'-error').fadeIn(500);
                 $('#submit').attr('disabled', 'disabled') }
               else
               { $(this.id.replace(/_/g, '-')+'-error').fadeOut(500);
                 $('#submit').attr('disabled', '') }
          });
     });
});

Anyone care to offer guidance to get this working?

Thanks,

Rick

Reply via email to