javascript - jQuery .on() running before value given -


why .on() not value of form input field (#forgot) before keypress has happened.

$(document).ready(function() {     $(document).on('keypress', '#pass', function() {     var value = $.trim($('#pass').val());         alert(value);         if (!value.length) {             alert("show");             $('#forgot').show();         } else {             alert("hide");             $('#forgot').hide();         }     }); }); 

when type in first character alert shows no input. second character leads value being first character. .on() function seems run before key press registered? how can fix or there alternative function?

simply change keypress keyup:

$(document).ready(function() {     $(document).on('keyup', '#pass', function() {     var value = $.trim($('#pass').val());         console.log(value);         if (!value.length) {             console.log("show");             $('#forgot').show();         } else {             console.log("hide");             $('#forgot').hide();         }     }); }); 

Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -