I have an input
element with an onkeypress
event that runs a function, change()
. The problem is that the function doesn't run for the very first keypress. For example, I have to type 55 instead of 5 to get the function to update, and the value it takes is 5. Somehow, it doesn't recognize the most recent keypress. I tried adding other events, such as onfocus
and onselect
, to no avail. Here is the code for the input
:
<input id="x" type="text" size="1" maxlength="2" onkeypress="change()" onfocus="change()" onselect="change()" onblur="change()" onchange="change()"></input>
I think the issue might be that the onkeypress
event triggers before the value is inserted.
If you want to capture the value after the key has been pressed you could use the onkeyup
event instead and see if that works better.