jQuery Keydown Event

Beginner
5 min read
Updated: Nov 2026
3 examples
Keyboard Events

Definition

The keydown event fires when the user presses a key. If the key stays down, it repeats at the OS key-repeat rate. Bind with .on("keydown") since jQuery 1.7; only the focused element receives it.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("#target").on("keydown", handler)   // bind (1.7+)
$("#target").trigger("keydown")       // fire handlers (1.0+)

event.which → key code
  Enter 13 · Esc 27 · Space 32 · arrows 37–40 · A–Z 65–90

keydown → may repeat while held
keyup   → once when the key comes up
CallWhat it does
KeydownLearn the jQuery keydown event: bind with .on(\

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Pick a focusable target — usually an <input> or <textarea>.
Step 2Bind with $("#target").on("keydown", handler) (jQuery 1.7+).
Step 3Read event.which; use preventDefault() when you block Enter or arrows.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Bind a Keydown Handler

Official style: log when a key is pressed while the input has focus.

jQuery
$("#target").on("keydown", function () {
  console.log("Handler for keydown called.");
});
Try It Yourself

How It Works

  1. Select the input. $("#target") must be able to receive focus.
  2. Bind keydown. .on("keydown", fn) registers the handler (jQuery 1.7+).
  3. Press a key. The handler runs immediately on press.

Example 2 — Trigger Keydown from a Button

Fire the bound handler programmatically with .trigger("keydown").

jQuery
$("#target").on("keydown", function () {
  console.log("Handler for keydown called.");
});

$("#other").on("click", function () {
  $("#target").trigger("keydown");
});
Try It Yourself

How It Works

  1. Bind first. Same handler as Example 1 on #target.
  2. Click the button. The click handler runs $("#target").trigger("keydown").
  3. Handlers fire. Bound keydown listeners run; this is not a full OS key simulation.

Example 3 — Count Presses and Block Enter

Official demo idea: count keydowns, show event.which, and preventDefault on Enter.

jQuery
var count = 0;

$("#target").on("keydown", function (event) {
  if (event.which === 13) {
    event.preventDefault();
  }
  count++;
  $("#log").text(
    "Called " + count + " time(s). Key: " + event.which
  );
});
Try It Yourself

How It Works

  1. Check which. 13 is Enter.
  2. preventDefault. Stops the default Enter action (e.g. form submit).
  3. Count every press. Including Enter — the handler still runs.

Frequently Asked Questions

keydown fires when the user presses a key. If the key is held, it repeats at the OS key-repeat rate. Bind with .on("keydown", handler) (jQuery 1.7+). Only the focused element receives it.
Read event.which — jQuery normalizes it across browsers. Common codes: 13 Enter, 27 Escape, 32 Space, 37–40 arrows, 65–90 letters A–Z.
keydown fires on press and can repeat while held. keyup fires once on release. Prefer keydown for shortcuts and movement; keyup when you react after the press finishes.
keydown fires for every key (arrows, Escape, modifiers). keypress mainly fires for character keys and is legacy/non-standard. Prefer keydown for shortcuts.
Bind .on("keydown", handler) on document. Key events bubble unless stopped. Combine event.which with event.ctrlKey / event.shiftKey / event.altKey.
Prefer .on("keydown", handler). The .keydown() shorthand is deprecated since jQuery 3.3. Fire with .trigger("keydown").

Next: .keydown() Method — Deprecated Event Shorthand

Learn the deprecated shorthand and how to migrate to .on("keydown").

.keydown() Method — Deprecated Event Shorthand tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful