Getting the value of a form field after keypress event

10,856

Solution 1

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

Solution 2

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.

Share:
10,856
morgancodes
Author by

morgancodes

Programmer/artist working on a monster javascript project for a cable television company for the past two and a half years. Meanwhile, I build magical sound experiences for iOS including Thicket, Morton Subotnick's Pitch Painter, and this fun toy payed for by gum. I also like to make geometric sculptures from paper. Future plans include releasing my C++ audio patching engine (build on top of STK) as an open source project, creating the world's most mesmerizing musical video game, building my own programming language, and finding a way to pay for it all.

Updated on June 07, 2022

Comments

  • morgancodes
    morgancodes almost 2 years

    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?