HTML onkeypress Event Attribute

Example

Execute a JavaScript block when the user presses a key:

<input type="text" onkeypress="displayResult()">

Try it yourself

Browser Support

IE Firefox Chrome Safari Opera

All major browsers support the onkeypress attribute.

Definition and Usage

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

Tip:Relative event order for onkeypress event:

  • onkeydown
  • onkeypress
  • onkeyup

Note:In any browser, the onkeypress event is not triggered by all keys (e.g., ALT, CTRL, SHIFT, ESC).

Note:The onkeypress attribute is not applicable to the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> or <title>.

Differences between HTML 4.01 and HTML5

None.

Syntax

<element onkeypress="script">

Attribute Value

Value Description
script Script executed when onkeypress event occurs.