Javascript- Onkeydown Event Handler


Javascript



The Onkeydown event handler in Javascript is an event handler that is called when a user presses down a key on the keyboard.

This web element can be any element where a user can enter a character into, such as a form element, such as a textbox or a text area.

If one of these elements has an onkeydown event handler and a key is pressed down on a keyboard, the event handler will be triggered and will do whatever it is programmed to do.

An example of the onkeydown event handler is shown below with the text box.

Click on this text box and press down any key on the keyboard.

Enter Your Name:

Once a key is pressed down, an alert pop-up box will then appear telling the user to "You Pressed a Key".

Code

The code to create the above text box which triggers the alert pop-up box is:

Enter Your Name: <input type="text" onkeydown="alert('You Pressed a Key');"/>

To create a text box that creates an alert pop-up when a key is pressed, we add an onkeydown event handler to the text box. The onkeydown event handler triggers the alert, "You Pressed a Key".

Note: The onkeydown event handler cannot be used with HTML elements where you cannot enter a key into, such as <div></div> tags or <body></body> tags. It must be an element where a key can be entered into such as a textbox or text area.


Related Resources

Javascript- Onload Event Handler

Javascript- Onunload Event Handler

Javascript- Onfocus Event Handler

Javascript- Onblur Event Handler

Javascript- Onclick Event Handler

Javascript- Onchange Event Handler

Javascript- Onkeyup Event Handler

Javascript- Onmouseover Event Handler

Javascript- Onmousedown Event Handler

Javascript- Onmouseup Event Handler

Javascript- Onmouseout Event Handler

Javascript- Onreset Event Handler

Javascript- Onabort Event Handler

Javascript- Oncopy Event Handler



HTML Comment Box is loading comments...