[jQuery] keypress vs keydown vs keyup SIMPLIFIED

So you’re learning jQuery and you just got to events. Congrats! But now this whole keypress, keydown, keyup thing is throwing you off. Don’t worry, I can help you separate those ideas.
Let’s first start with keydown and keyup since they are easier. They are pretty self-explanatory. When a key is pressed DOWN, the keydown event is sent to an element. If the key is kept down, that event is sent to the element each time the character is repeated. This means if you have a text box and you hold down the ‘a’ key for two seconds and five a’s appear, a keydown event would have been sent 5 times if it had been listening properly to that textbox. Keyup works much in the same way except the keyup event is only sent when a key is let go of.
Now the tricky part, on to the keypress event. The keypress event is sent to an element when a key pressed. For example, I can add a listener for the key ‘A’ rather than ‘a’. The keypress allows the user to listen for other characters that may be modified using other keys such as the shift key. Take a look at this html/js snippet: code snippet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s