Example

Change the background color of an input field each time a key is pressed:

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

Try it yourself »

Definition and Usage

A key press has two parts: 1. The key is pressed down. 2. The key is released.

The keydown event occurs when a keyboard key is pressed down.

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

Tip: Use the event.which property to return which key was pressed.


Trigger the keydown Event

Trigger the keydown event for the selected elements.

Syntax

$(selector).keydown()
Try it yourself »


Bind a Function to the keydown Event

Specifies a function to run when the keydown event occurs for the selected elements.

Syntax

$(selector).keydown(function)
Try it yourself »

Parameter Description
function Optional. Specifies the function to run when the keydown event occurs


Examples

Try it Yourself - Examples

Determine which key was pressed
How to determine which key was pressed.