![]() |
The
onkeydown event attribute in HTML is a global event attribute that triggers a script when a user presses a key on the keyboard. This event fires as soon as the key is pressed down, before it is released. |
Example:
HTML
<input type="text" onkeydown="myFunction()">
The onkeydown attribute fires when the user is pressing a key (on the keyboard).
Tip: The order of events related to the onkeydown event:
<element onkeydown="script">
Value | Description |
---|---|
script | The script to be run on onkeydown |
Supported HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|
A function is triggered when the user is pressing a key in the input field.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user is pressing a key in the input field.</p>
<input type="text" onkeydown="myFunction()">
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
Press and hold down a key inside the text field to set a red background color. Release the key to set a green background color.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p>Press and hold down a key inside the text field to set a red background color. Release the key to set a green background color.</p>
<input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()">
<script>
function keydownFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
function keyupFunction() {
document.getElementById("demo").style.backgroundColor = "green";
}
</script>
</body>
</html>
html event attributes |
List of Keyboard Events Attribute | Keyboard Events Attribute |
HTML onkeydown Event Attribute | Keyboard Events Attribute |
HTML onkeypress Event Attribute | Keyboard Events Attribute |
HTML onkeyup Event Attribute | Keyboard Events Attribute |
Example of HTML onkeydown Event Attribute | Keyboard Events Attribute |
Definition and Usage of HTML onkeydown Event Attribute | Keyboard Events Attribute |
Browser Support of HTML onkeydown Event Attribute | Keyboard Events Attribute |
Syntax of HTML onkeydown Event Attribute | Keyboard Events Attribute |
Attribute Values of HTML onkeydown Event Attribute | Keyboard Events Attribute |
Technical Details of HTML onkeydown Event Attribute | Keyboard Events Attribute |
How to Execute a JavaScript when a user is pressing a key | Keyboard Events Attribute |
How to Use "onkeydown" together with the "onkeyup" attribute | Keyboard Events Attribute |
Type: | Develop |
Category: | Web Tutorial |
Sub Category: | Keyboard Events Attribute |
Uploaded by: | Admin |
Reffered: https://www.w3schools.com/tags/ev_onkeydown.asp