jQuery keydown() method
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:
- keydown - the process of a key being pressed
- keypress - the key is pressed
- 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