Javascript, key press value is always one character behind the latest?

Tom picture Tom · Aug 17, 2010 · Viewed 7.8k times · Source

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?

Answer

lincolnk picture lincolnk · Aug 17, 2010

use the keyup event instead of keypress. keydown will show the before-keystroke value, as will keypress (apparently).