Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the value of a form field after keypress event

myInput.value is one keystroke behind when I examine it in a keyPress event handler. So if the users types "a", myInput.value gives me "". Then when the user types "b", myInput.value gives me "a", and so on. The value doesn't seem to get updated with the character input by the keystroke that triggered the event. What am I doing wrong?

like image 624
morgancodes Avatar asked Feb 11 '09 16:02

morgancodes


2 Answers

Use the keyUp event, it should definitely give you the value you are looking for.

like image 82
sandesh247 Avatar answered Oct 11 '22 11:10

sandesh247


When I had this problem, the thing I actually wanted was the oninput event handler.

One notable difference between this and the keyUp solution is that if you e.g. hold down a key to repeat it, oninput will get called every time the key repeats, but keyUp will only get called when the key is released.

like image 25
Ben Millwood Avatar answered Oct 11 '22 11:10

Ben Millwood