JQuery: how to find elements * without * a specific class

Why this fails ...

$( 'div.contactAperson input' ).not( 'input.hadFocus' ).focus(function() { $(this).attr('value', '' ); }); 

... this meant sniffing out the input that didn't receive the .hadFocus class, and then when one of these subsets gets focus, it should replace the value with null.

Currently, input values ​​always get zapped - the .not test ('input.hadFocus') cannot stop execution.

Btw, preceding the above code, is the following code that works fine:

 $( 'div.contactAperson input' ).focus(function() { $( this ).addClass( 'hadFocus' ); }); 

Thanks for any trick - greetings, -Alan

+6
jquery-selectors
source share
2 answers

You need a handler to run based on the current state of the element - not the state when it was bound. You probably need to use live binding.

Try something like this:

 $('div.contactAperson input:not(.hadFocus)').live('focus', function() { $(this).attr('value', '' ); }); 
+5
source share
 $( 'div.contactAperson > :input' ).not( ':input.hadFocus' ).focus(function() { $(this).attr('value', '' ); }); 

luck

+4
source share

All Articles