Horje
HTML onkeypress Event Attribute
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 of HTML onkeypress Event Attribute

It will Execute a JavaScript when a user presses a key.
index.html
Example: HTML
 <input type="text" onkeypress="displayResult()"> 

Output should be:

Example of HTML onkeypress Event Attribute

Definition and Usage HTML onkeypress Event Attribute

The onkeypress attribute fires when the user presses a key (on the keyboard).

Tip: The order of events related to the onkeypress event:

  1. onkeydown
  2. onkeypress
  3. onkeyup

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

Browser Support of HTML onkeypress Event Attribute

Browser Support of HTML onkeypress Event Attribute

Syntax of HTML onkeypress Event Attribute

<element onkeypress="script">

Attribute Values of HTML onkeypress Event Attribute

Value Description
script The script to be run on onkeypress

Technical Details of HTML onkeypress Event Attribute

Supported HTML tags: All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>

How to Execute a JavaScript when a user presses a key

A function is triggered when the user is pressing a key in the input field.

index.html
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>

Output should be:

How to Execute a JavaScript when a user presses a key




html event attributes

Related Articles
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

Single Articles
Example of HTML onkeypress Event AttributeKeyboard Events Attribute
Definition and Usage HTML onkeypress Event AttributeKeyboard Events Attribute
Browser Support of HTML onkeypress Event AttributeKeyboard Events Attribute
Syntax of HTML onkeypress Event AttributeKeyboard Events Attribute
Attribute Values of HTML onkeypress Event AttributeKeyboard Events Attribute
Technical Details of HTML onkeypress Event AttributeKeyboard Events Attribute
How to Execute a JavaScript when a user presses a keyKeyboard Events Attribute

Type:
Develop
Category:
Web Tutorial
Sub Category:
Keyboard Events Attribute
Uploaded by:
Admin


Reffered: https://www.w3schools.com/tags/ev_onkeypress.asp