HTML | onkeydown Event Attribute
This onkeydown event attribute works when user presses any key from the keyboard.
Syntax:
<element onkeydown = "script">
Attribute Value: This attribute contains single value script which works when any key pressed from the keyboard.
Supported Tags: It is supported by all HTML elements except <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.
Example:
<!DOCTYPE html> <html> <head> <title> HTML | onkeydown Event Attribute </title> <style> h1 { text-align: center; color: green; } h2 { text-align: center; } input[type=text] { width: 100%; padding: 12px 20px; margin: 8px 0; box-sizing: border-box; font-size: 24px; color: white; } p { font-size: 20px; } </style> </head> <body> <h1>GeeksforGeeks<h1> <h2>onkeyup Event Attribute</h2> <p>Release the key to set a green background color.</p> <input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()"> <script> function keyupFunction() { document.getElementById("demo").style.backgroundColor = "blue"; } function keydownFunction() { document.getElementById("demo").style.backgroundColor = "green"; } </script> </body> </html> |
Output:
Press the key:

Release the key:

Supported Browser: The browser supported by onkeydown event attribute are listed below:
- Chrome
- Internet Explorer
- Firefox
- Safari
- Opera
Recommended Posts:
- HTML | ondrag Event Attribute
- HTML | onoffline Event Attribute
- HTML | onsubmit Event Attribute
- HTML | onfocus Event Attribute
- HTML | ondrop Event Attribute
- HTML | onpaste Event Attribute
- HTML | onresize Event Attribute
- HTML | onhashchange Event Attribute
- HTML | onreset Event Attribute
- HTML | onsearch Event Attribute
- HTML | onpageshow Event Attribute
- HTML | ondragenter Event Attribute
- HTML | oninvalid Event Attribute
- HTML | onselect Event Attribute
- HTML | onbeforeprint Event Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



