jQuery Event - keydown() Method

Example

Change the text field color when the key is pressed:

$("input").keydown(function(){
  $("input").css("background-color","#FFFFCC");
});

Try it yourself

Definition and usage

The complete key press process is divided into two parts: 1. The key is pressed; 2. The key is released.

A keydown event occurs when a button is pressed.

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

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

Tips:Please use .which propertyTo determine which key is pressed (Try it yourself).

Triggers a keydown event

Syntax

$(selector).keydown()

Try it yourself

Binds the function to the keydown event

Syntax

$(selector).keydown(function)
Parameters Description
function Optional. Specifies the function to be executed when a keydown event occurs.

Try it yourself