Javascript KeyDown
The javascript onkeydown event occurs when the user is pressing a key (on the keyboard).
Syntax:
In HTML:
<element onkeydown="myScript">
In JavaScript:
object.onkeydown = function(){myScript};
EXAMPLE:
<!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>
OUTPUT:
