1

I'm having an issue with removing classes from a HTML input when that element becomes empty.

Please see: http://jsfiddle.net/GgFXs/4/

This happens for a split second, or if you hold the backspace button in the field. Any time after, the field takes on the 'valid' class - why? and how to fix?

3
  • I'm unfamiliar with the input event - are you sure that's the event you meant to bind? Commented Jun 12, 2011 at 20:01
  • yes, i use 'input' because it fires on all key presses + pastes Commented Jun 12, 2011 at 20:03
  • I think it's working for me. When I erased all my input, the background turned white again. Is that not the behavior you want? edit: actually, I take that back - I had put an alert in there and I think that caused a different timing condition - when I removed it the background stayed green. I'll have to look at it more. Commented Jun 12, 2011 at 20:10

2 Answers 2

2

Your logic is quite convoluted. You occasionally end up with the undesirable situation of having both .valid and .invalid applied. Try this instead:

$(function() {
    $('form').validate();
    $('input').bind('input', function() {
        if ($(this).val() != '') $(this).addClass('nonEmpty');
        else $(this).removeClass('nonEmpty');

        if ($(this).valid()) $(this).addClass('valid');
        else $(this).removeClass('valid');
    });
});

.nonEmpty {background: #E4CCCC;}
.nonEmpty.valid {background: #9BCC60;}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it works - but can you explain why my logic is 'quite convoluted', and why my method was not working?
+1, and note that the onInput event is not supported in IE 8 or lower. It works in IE 9. help.dottoro.com/ljhxklln.php
@Jordan: Well for one, your code allows an input to be both valid and invalid! It's hard to tell what's the cause of the persisting valid state.
Oops, Indeed - I forgot to call .removeClass() on the selectors to remove the opposite class. I've made this change on jsfiddle but the valid state persists. If anybody can explain why, please do. @Will Martin: thanks for this.
1

You're mixing up functionality from the Validation plugin with your own code. You can remove the valid className, but the Validation plugin will add it back if it thinks the field is valid.

If you want a blank field to be rejected by the Validation plugin, you'll have to give it class="required". If you want to be monitoring, adding and removing classes manually, you're not really using the Validation plugin as intended IMO.

1 Comment

this is perfect, thanks for pointing out that the validation plugin reserves the .valid and .invalid class names. I did not realize this. Changing the class names to my_valid and my_invalid and adding the removeClass()'s worked. Final working code here: jsfiddle.net/8QvXP/1 Thanks all.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.