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

jQuery keydown() method

jQuery Events

The keydown() method triggers the keydown event, or attach a function to run when the keydown event occurs.

A keydown event occurs when the user presses a key.

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

Useevent.whichThe attribute returns the pressed keyboard key.

Syntax:

Trigger the keydown event of the selected element:

$(selector).keydown()

Attach the function to the keydown event:

$(selector).keydown(function)

Example

A popup alert will appear when the user presses a key in the <input> field:

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

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

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

Determine which key was pressed:

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

Trigger the keydown event of the <input> field:

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

Parameter Value

ParameterDescription
functionFunction executed when a key event is triggered

jQuery Events