The onkeydown
event is working properly.
When a key is pressed, the events are fired in the following sequence; keydown
, keypress
, keyup
.
The onkeydown
event is fired after the key press but before the character is put into the DOM.
Since the character has not been put into the DOM when the event is fired your form text input will not have that character inside of it.
For example: the onkeydown
event can be used to restrict characters from being inputted into your form.
On the other hand: the onkeyup
event is fired after the user has released the key and after the DOM has been modified.
Perhaps the onkeyup
event is better suited for your application.