Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery keydown() method

jQuery keydown() method

jQuery event methods jQuery event methods

Example

When a keyboard key is pressed, set the field's background color:

$

(

"

input

"

)

.

keydown

(

function

(

)

{

$

(

"

input

"

)

.

css

(

"

background-color

"

,

"

yellow

"

)

;

}

)

;

Try it »


Definition and Usage

Event sequence related to the keydown event:

  1. keydown - the process of a key being pressed
  2. keypress - the key is pressed
  3. keyup - the key is released

The keydown event occurs when a keyboard key is pressed.

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

**Tip:** Please use event.which property to return which keyboard key was pressed.


Syntax

Trigger the keydown event of the selected element:

$(

selector

).keydown()

Try it

Add a function to the keydown event:

$(

selector

).keydown(

function

)

Try it

Parameters Description
function Optional. Specifies the function to run when the keydown event is triggered.

More examples

Determine which key was pressed
How to use the event.which property to determine which key was pressed.


jQuery event methods jQuery event methods