Attach your handler to the keyrelease
event. The value should have been updated by then.
views:
42answers:
6 $('.A, .B').keydown(function(event) {
if(!empty($('.A').val()) && !empty($('.B').val()))
$(".C").attr("disabled","");
else
$(".C").removeAttr("disabled");
});
At the keypress
event, the value of the INPUT
is not yet set. Hence you can't easily know if it is empty.
While the keyup
fires, the value of the INPUT
is set.
But things get worse. The user can empty the field with the mouse or the Edit menu of the browser. In that case keyup
is not fired.
In our web app we auto-save the values.
Like in Mac OS, if you know, there is almost no save buttons.
To allow a reaction here is more or less what we do:
onfocus
: asetInterval
starts polling every 120ms or so, and checks the input for any change- if there is a change do the relevant action
onblur
: aclearInterval
stop the polling
use a combination of handlers for keyup
and change
. the keyup
handler will update as the user types (excepting edge cases like holding a key down, which doesn't seem like a concern here) and the change
handler will catch things like the user cutting the text with mouse actions before they can switch to field C. as an added measure you could add verification on field C's focus
event, to make sure A and B really have something.