Last Updated : 12 Jul, 2025
The DOM onkeydown event in HTML occurs when a key is pressed by the user. Order of events related to the onkeydown event:
Supported HTML tags: All HTML elements, EXCEPT:
Syntax:
<element onkeydown="myScript">>
object.onkeydown = function(){myScript};
object.addEventListener("keydown", myScript);
Example: onkeydown Event using the addEventListener() method
html
<!DOCTYPE html>
<html>
<body>
<h1 style="color:green">
GeekforGeeks
</h1>
<p>
Press any key inside the input field
</p>
<input type="text" id="inputField"
style="background-color:green">
<script>
document.getElementById(
"inputField").addEventListener("keydown", GFGFun);
function GFGFun() {
document.getElementById(
"inputField").style.backgroundColor =
"yellow";
}
</script>
</body>
</html>
Output:
Supported Browsers: The browsers supported by HTML DOM onkeydown Event are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4