jQuery keydown()Method
Example
Set the background color of an <input> field when a keyboard key is pressed:
$("input").keydown(function(){
$("input").css("background-color","yellow");
});
Try it Yourself »
Definition and Usage
Event order related to the keydown event:
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 useevent.whichproperty to return which keyboard key was pressed.
Syntax
Trigger the keydown event for the selected element:
$(selector).keydown()
Try it Yourself
Add a function to the keydown event:
$(selector).keydown(function)
Try it Yourself
| Parameter | Description |
|---|---|
| function | Optional. Specifies the function to run when the keydown event is triggered. |
More Examples
Determine which key is pressed
How to use the event.which property to determine which key is pressed.
Other Extensions
jQuery Event Methods