onkeypress event
Example
Execute JavaScript code when the user presses a keyboard button:
Try it »
Definition and Usage
The onkeypress event occurs when a keyboard key is pressed and released.
**Tip:** The order of events associated with the onkeypress event:
- onkeydown
- onkeypress
- onkeyup
Note: In all browsers, the onkeypress event can only listen for letters and numbers, not some special keys (ALT, CTRL, SHIFT, ESC, arrows, etc.). To listen for whether a user presses a key, please use onkeydown the event; all browsers support the onkeydown event.
Syntax
In HTML:
<
element
onkeypress="
SomeJavaScriptCode
">In JavaScript:
object
.onkeypress=function(){
SomeJavaScriptCode
};
| Parameters | Description |
|---|---|
| SomeJavaScriptCode | Required. Specifies the JavaScript to be executed when the event occurs. |
Browser support





All major browsers support the onkeypress event
HTML tags that support the onkeypress event:
The onkeypress attribute can be applied to all HTML elements, except:
, , ,
Event Object