onkeydown and onkeyup in Javascript

What are onkeydown and onkeyup?

Examples

Example 1:

What's going on here?

  1. HTML allows for the input tag, which allows the user to enter certain types of data.



  2. When a user presses a key down, the text box's background turns purple. This is the onkeydown event in action.



  3. However, to merely describe the event in a function is not enough. The function itself must be called. When the function is called, it looks like this:


  4. When a user releases a key, the text box's background turns pink. This is the onkeyup event in action. It is written in the same fashion as onkeydown:


  5. FULL CODE

Example 2:

What's going on here?

  1. The onkeydown and onkeyup events are still being used within the confines of a text box. However, this time, the text box's borders are being targeted, not the text box's background color. We can see this changes inside the "down" and "up" functions:



  2. The callings of the functions also look identical to the first example:



  3. FULL CODE

Example 3:

What's going on here?

  1. Instead of using a "text" element, the type of input element being used here is called "password," because it masks the characters that the user is typing. In HTML, it looks like this:


  2. Now, onkeydown and onkeyup functions are targeting the password box's width and height. The up and down functions also look different; they are calling more than one action per event. Notice, however, that the callings of the functions are still the same as the first two examples:


  3. FULL CODE

More Resources:

  1. W3 Schools: onkeyup
  2. W3 Schools: onkeydown
  3. W3 Schools: Input Types