Attribute | Value | Description |
---|---|---|
onkeydown | script | Fires when a user is pressing a key |
onkeypress | script | Fires when a user presses a key |
onkeyup | script | Fires when a user releases a key |
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>
onkeypress
event attribute in HTML triggers a script when a user presses a key on the keyboard. This attribute is typically used within HTML elements that can receive keyboard input, such as <input>
fields or <textarea>
elements.Example:
HTML
<input type="text" onkeypress="displayResult()">
The onkeypress attribute fires when the user presses a key (on the keyboard).
Tip: The order of events related to the onkeypress event:
Note: The onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers. To detect only whether the user has pressed a key, use onkeydown instead, because it works for all keys
<element onkeypress="script">
Value | Description |
---|---|
script | The script to be run on onkeypress |
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" onkeypress="myFunction()">
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
onkeyup
event attribute is used to execute a script or function when a user releases a key on the keyboard. This attribute can be applied to various HTML elements, commonly input fields and text areas, to enable dynamic responses to user input.
Example:
HTML
<input type="text" onkeyup="myFunction()">
The onkeyup attribute fires when the user releases a key (on the keyboard).
Tip: The order of events related to the onkeyup event:
<element onkeyup="script">
Value | Description |
---|---|
script | The script to be run on onkeyup |
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 releases a key in the input field. The function transforms the character to upper case.
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when the user releases a key in the input field. The function transforms the character to upper case.</p>
Enter your name: <input type="text" id="fname" onkeyup="myFunction()">
<script>
function myFunction() {
let x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>