jQuery Event - keypress() Method

Example

Calculate the number of key presses in the input field:

$("input").keydown(function(){
  $("span").text(i+=1);
});

Try It Yourself

Definition and Usage

The keypress event is similar to the keydown event. It occurs when a button is pressed. It happens on the currently focused element.

However, unlike the keydown event, a keypress event occurs each time a character is inserted.

The keypress() method triggers a keypress event or specifies a function to be executed when a keypress event occurs.

Note:If set on a document element, this event will occur regardless of whether the element is focused or not.

Triggers a keypress event

Syntax

$(selector).keypress()

Try It Yourself

Binds a function to the keypress event

Syntax

$(selector).keypress(function)
Parameter Description
function Optional. Specifies a function to be executed when a keypress event occurs.

Try It Yourself