HTML onkeypress Event Attribute
Example
Run a script when a key is pressed:
<input type="text" onkeypress="displayResult()">Try it »
Browser support





All major browsers support the onkeypress event attribute
Definition and Usage
The onkeypress attribute is triggered when a key is pressed.
Tip: The event firing order related to the onkeypress event:
- onkeydown
- onkeypress
- onkeyup
Note: The onkeypress event cannot trigger all keys in all browsers (e.g., ALT, CTRL, SHIFT, ESC). If you only want to detect whether the user has pressed a key, you can use onkeydown instead; onkeydown is triggered by all keys.
Note: The onkeypress attribute cannot be used with the following elements:
, , ,
HTML Event Attributes