![]() |
The
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>
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 onkeypress Event Attribute | Keyboard Events Attribute |
Definition and Usage HTML onkeypress Event Attribute | Keyboard Events Attribute |
Browser Support of HTML onkeypress Event Attribute | Keyboard Events Attribute |
Syntax of HTML onkeypress Event Attribute | Keyboard Events Attribute |
Attribute Values of HTML onkeypress Event Attribute | Keyboard Events Attribute |
Technical Details of HTML onkeypress Event Attribute | Keyboard Events Attribute |
How to Execute a JavaScript when a user presses a key | Keyboard Events Attribute |
Type: | Develop |
Category: | Web Tutorial |
Sub Category: | Keyboard Events Attribute |
Uploaded by: | Admin |
Reffered: https://www.w3schools.com/tags/ev_onkeypress.asp