HTML event onkeypress

From Htmlpedia
Jump to: navigation, search

Event Attribute onKeyPress

onKeyPress is an event attribute, it takes JavaScript-code as a value. The code will be executed when:

  • The element is focused and a key is pressed


In this example, the user will receive a messagebox saying "You pressed a key over the text", after focusing on the element and pressing a key.

<input type="text" onkeypress="alert('You pressed a key over the text')" value="Focus and press a key" />


Users may find it difficult to focus on an element such as an anchor tag. Form elements like textarea and input are very suitable for this event though.


onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup