If I type 'St', by the time I press the t, if I output the input of textfield.value
in the onkeypress
/onkeydown
functions, I only get 'S'.
Why is this? How do I get rid of this lag?
If I type 'St', by the time I press the t, if I output the input of textfield.value
in the onkeypress
/onkeydown
functions, I only get 'S'.
Why is this? How do I get rid of this lag?
Because the keystroke is not registered until keyup event occurs. So you should detect
onkeyup
event instead ofonkeypress
.Within the
keypress
event, it's still possible to prevent the typed character from registering, so the input's value canot be updated until after thekeypress
event. You can use thekeyup
event instead, or usewindow.setTimeout()
to set up a delay.use the
keyup
event instead ofkeypress
.keydown
will show the before-keystroke value, as willkeypress
(apparently).