I’m trying to add a class (.active) to a text field once the user starts typing. I got it to work somewhat with the following code, but for some reason the .active class is not applied immediately when the user starts typing, it’s only applied after a second letter has been typed. Any ideas?
$(document).ready(function() {
loginField = $('.field');
loginField.live('keydown', function(){
if ($(this).val()){
$(this).addClass('active');
}
});
});
You want
keyuphere, updated based on comments:Your
.live()is firing correctly, but the.val()doesn’t change untilkeyupfires,keydownfires before the value is updated, so yourif()isn’t true until the 2nd key is pressed.