jQuery: Function call on keydown but get value on keyup

Is there anyway to call a function on keydown but only get the value of a certain element once the key has been released? Basically all the other functions in my code need to be called on keydown except for one which needs the value of an input field after the key has been released. So to keep things simple I’d like to be able to have the default case in my switch statement call this function which then waits until the keyup event is fired.

Add Comment
1 Answer(s)

You can’t wait for events, as event handlers only start while other code is not running, so you would have to store the information for that key somewhere and use it in the keyup event.

Note that each keydown eventually gets a keyup, but not necessarily in the same order, e.g.:

shift down
space down
shift up
K down
space up
K up

So, if you keep information from the keydown event, you would have to store it separately for each key.

Consider also using the keypress event that happens when a keypress produces a character, which also happens when keys are repeating when you hold it down.

Answered on July 12, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.