Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML onkeypress Event Attribute

HTML onkeypress Event Attribute

HTML Event Attributes HTML Event Attributes

Example

Run a script when a key is pressed:

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

Try it »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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:

  1. onkeydown
  2. onkeypress
  3. 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: , ,
, , ,