6

Why does this fail...

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

...it's meant to sniff out input's that have not got the class .hadFocus and then when one of that subset receives focus it should zap the value to null.

Right now, input values are always getting zapped -- the test .not( 'input.hadFocus' ) is failing to stop execution.

Btw, preceding the above code is the following code, which is working fine:

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

Thanks for any cleverness - cheers, -Alan

1

2 Answers 2

5

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

Try something like this:

$('div.contactAperson input:not(.hadFocus)').live('focus', function() {
    $(this).attr('value', '' );
});
1
  • Thanks, sound like that'll be it. Now just gotta find the project this related to. Cheers, -Alan
    – Alan
    May 31, 2010 at 12:03
4
$( 'div.contactAperson > :input' ).not( ':input.hadFocus' ).focus(function() {
    $(this).attr('value', '' );
});

good luck

1
  • Tks, dunno why but this version never clears the value. But tks for the code suggestion, I'll keep scratching away at it...
    – Alan
    Apr 26, 2010 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.