jQuery keypress() Method

jQuery keypress() method trigger the keypress event. The jQuery keypress event occurs when a keyboard key is pressed down. And when the keypress event occurs, specified function will execute.

Syntax

This triggers the keypress event on the selected elements.

$(selector).keypress();        // This syntax does not accept any arguments.

Attach a function to the keypress event.

$(selector).keypress(function);
Parameter Type Description
function Function Optional. Specifies the function to execute when the keypress event is occurs.

Examples

This example represents the keypress() event.

$(document).ready(function(){
  i = 0;
  $("input").keypress(function(){
    $("span").text(i=i+1);
  });
  $("button").click(function(){
    $("input").keypress();
  });
});

Run it...   »


This example represent to attach a function to the keypress() event.

$(document).ready(function(){
  i = 0;
  $("input").keypress(function(){
    $("span").text(i=i+1);
  });
});

Run it...   »