HTML onkeypressEvent Attribute

HTML 事件属性HTML Event Attributes

Examples

Run a script when a key is pressed:

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

Try it yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the onkeypress event attribute.


Definition and Usage

The onkeypress attribute triggers when a key is pressed.

Tip:The order of events related to the onkeypress event:

  1. onkeydown
  2. onkeypress
  3. onkeyup

Note:The onkeypress event does not trigger for all keys (e.g., ALT, CTRL, SHIFT, ESC) in all browsers. If you only need to detect whether the user has pressed a key, you can useonkeydownInstead, onkeydown is triggered by all keys.

Note:The onkeypress attribute cannot be used on 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 Values

Value Description
script Specifies the script to be executed when the onkeypress event is triggered.


HTML 事件属性HTML Event Attributes Other Extensions