HTML onkeydown Attribute

HTML onkeydown Attribute: This attribute is defined as it fires when the user presses a key. And this attribute contains a single value script which works when any key pressed from the keyboard. While the order of events related to the onkeydown event:

  • onkeydown
  • onkeypress
  • onkeyup

HTML onkeydown Attribute

This attribute can be applied to all the HTML elements.

Supported Tags: It is supported by all HTML elements except <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.

Syntax: <element onkeydown = “script”>

Browser Support:

This attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Safari
  • Opera
  • Internet Explorer

Example: for <input> element

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user presses a key </p>
<input type="text" onkeydown="myFunction()">
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <p>A function is triggered when the user presses a key </p> <input type="text" onkeydown="myFunction()"> <script> function myFunction() { alert("You pressed a key inside the input field"); } </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user  presses a key </p>
<input type="text" onkeydown="myFunction()">
<script>
function myFunction() {
  alert("You pressed a key inside the input field");
}
</script>
</body>
</html>

Output:

A function is triggered when the user is pressing a key in the input field.