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

onkeypress event

onkeypress event

Event Object Reference Event Object

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:

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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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:, ,
, , ,