I came across this question: onKeyPress Vs. onKeyUp and onKeyDown, from where I found that keypress
is supposed to fire whenever a character is typed into the text input. I am trying to run this following code. It is supposed to make the input's background yellow the moment its text length exceeds 0, or white the moment it is 0. I can't make it work. If I try to do keydown
, I have the following problems:
If I just type one character and let go, the background remains white.
If then, I press
backspace
, thus clearing that one character, it turns yellow (just opposite of what I want!). If I press any other key now (Alt
,Shift
) it will turn white again. In fact, if instead ofAlt
orShift
I type a character, it will still remain white, taking us back to the first problem.If I type press a character key and keep it pressed, the background remains white for the first character, and turns yellow 2nd character onwards.
If I try keyup
only, these are the problems (as expected):
- The background doesn't change as long as the keys are kept pressed, even when a character is added to the empty input or the entire text deleted.
If I try keypress
, I face the same problems as keydown
, even though it is supposed to work.
If I bind 3 handlers for keyup
, keydown
and keypress
(God I am desperate!), almost all problems are solved except problem 3 of keydown
: if I type press a character key and keep it pressed, the background remains white for the first character, and turns yellow 2nd character onwards.
How do I solve this problem?
JS:
$(document).ready(function() {
$("input").bind("keydown", function() {
if ($(this).val().length == 0) {
$(this).css('background', 'white');
} else {
$(this).css('background', 'yellow');
}
});
$("input").bind("keypress", function() {
if ($(this).val().length == 0) {
$(this).css('background', 'white');
} else {
$(this).css('background', 'yellow');
}
});
$("input").bind("keyup", function() {
if ($(this).val().length == 0) {
$(this).css('background', 'white');
} else {
$(this).css('background', 'yellow');
}
});
});
HTML:
<input type='text' />