English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery keypress() method

jQuery Events

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

Key events are similar to key events. This event occurs when a key is pressed.

However, the keypress event will not be triggered for all keys (such as ALT, CTRL, SHIFT, ESC, BACKSPACE).

Note:The order of events related to key events is:

Useevent.whichThe attribute returns the pressed keyboard key.

Syntax:

Trigger the key event of the selected element:

$(selector).keypress()

Attach the function to the key event:

$(selector).keypress(function)

Example

An alert will pop up when a key is pressed in the <input> field:

$("input").keypress(function(){
  alert("Welcome to the Basic Tutorial Online www.oldtoolbag.com!!!");
});
Test and see‹/›

Set the background color of the <input> field when a keyboard key is pressed:

$("input").keypress(function(event){
  $("background-color", "yellow");
  $("span").text(event.type);
});
Test and see‹/›

Determine which key was pressed:

$("input").keypress(function(event){ 
  $("div").text("Key:  " + event.which);
});
Test and see‹/›

Trigger the keypress event of the <input> field:

$("#btn1").click(function(){
  $("input").keypress();
});
Test and see‹/›

Parameter Value

ParametersDescription
functionFunction executed when a key event is triggered

jQuery Events