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

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


Read Article
https://develop.horje.com/learn/1434/reference