jQuery .keydown() Method

Beginner
Deprecated 3.3
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .keydown()?

.keydown() is jQuery’s legacy shorthand for the keydown event (since 1.0). Pass a handler to bind; call it with no arguments to trigger. Binding forms are deprecated since jQuery 3.3.

Remember
$("#el").keydown(fn)           // bind (deprecated)
$("#el").keydown(data, fn)     // bind + eventData (deprecated)
$("#el").keydown()             // trigger handlers

// Modern replacements:
$("#el").on("keydown", fn)
$("#el").trigger("keydown")
NeedPrefer
Bind keydown.on("keydown", fn)
Trigger keydown.trigger("keydown")
Read legacy code.keydown(fn) / .keydown()
On release.on("keyup", fn)

How to Use .keydown()

Recognize the shorthand in old code, then migrate to .on / .trigger.

  1. Find .keydown(fn) or bare .keydown() in legacy scripts.
  2. Rewrite binds as .on("keydown", fn) (add eventData as the middle arg if needed).
  3. Rewrite triggers as .trigger("keydown").
  4. Keep using event.which for key codes — that part does not change.
PieceRequired?Role
handlerFor bindfunction(event) { … }
eventDataOptionalObject → event.data (since 1.4.3)
No args—Trigger keydown on matched elements
Return—jQuery object (chainable)

Pseudocode

Pseudocode
if called with handler:
    // deprecated since 3.3
    bind handler to "keydown" on each element
    (same as .on("keydown", handler))

if called with (eventData, handler):
    bind with data → event.data
    (same as .on("keydown", eventData, handler))

if called with no arguments:
    fire "keydown" on each matched element
    (same as .trigger("keydown"))

Cheat Sheet

Legacy vs modern — keep this table for quick migrations.

LegacyModern
$("#el").keydown(fn)$("#el").on("keydown", fn)
$("#el").keydown(data, fn)$("#el").on("keydown", data, fn)
$("#el").keydown()$("#el").trigger("keydown")
DelegationOnly via .on("keydown", "input", fn)
Unbind$("#el").off("keydown")
StatusDeprecated since jQuery 3.3

Live Preview

Compare the deprecated shorthand with the modern API. Press a key (hold to see repeat), or click Trigger to fire handlers.

Browser demo of .keydown() vs .on("keydown") — same event underneath.

Result
$("#field").keydown(fn)  // deprecated bind
→ press a key to fire

/* prefer .on("keydown", fn) */

jQuery Examples

Short demos of the legacy shorthand and how to migrate. Prefer .on / .trigger in new code.

Example 1 — Bind with .keydown(handler)

Legacy bind — runs when a key is pressed while the input has focus.

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

How It Works

  1. Select the input. $("#target") must be focusable.
  2. Call the shorthand. .keydown(fn) registers the handler (internally uses .on).
  3. Press a key. The handler runs on press; hold may repeat.
  4. Prefer modern syntax. New code should use .on("keydown", fn).

Example 2 — Pass eventData

Since 1.4.3, pass an object before the handler — it arrives as event.data.

jQuery
$("#search").keydown({ fieldId: "search" }, function (event) {
  $("#out").text(
    "Field: " + event.data.fieldId + " | key: " + event.which
  );
});
Try It Yourself

How It Works

  1. Pass data first. { fieldId: "search" } is stored with the handler.
  2. Read event.data. On keydown, the object is available without a closure.
  3. Use event.which. Normalized key code (e.g. 65 for A).
  4. Migrate. Same pattern with .on("keydown", data, fn).

Example 3 — Trigger with No-Arg .keydown()

Calling .keydown() without a handler fires bound keydown listeners.

jQuery
var count = 0;

$("#target").keydown(function () {
  count++;
  $("#out").text("Fired " + count + " time(s)");
});

$("#fire").click(function () {
  $("#target").keydown();
});
Try It Yourself

How It Works

  1. Bind first. A handler is registered with the shorthand.
  2. Click fires it. Bare .keydown() triggers the event.
  3. Handlers run. Not a full OS key simulation.
  4. Clearer API. Use .trigger("keydown") in new code.

Example 4 — Side-by-Side Migration

Same behavior — deprecated shorthand vs modern .on / .trigger.

jQuery
// Legacy
$("#legacy").keydown(function () {
  console.log("via .keydown(fn)");
});

// Modern (prefer this)
$("#modern").on("keydown", function () {
  console.log("via .on(\"keydown\", fn)");
});
Try It Yourself

How It Works

  1. Two equivalent binds. Shorthand and .on attach the same event type.
  2. No behavior change. Migration is naming, not a rewrite of handler logic.
  3. .on unlocks delegation (.on("keydown", "input", fn)).
  4. One style going forward. Stick to .on / .trigger / .off.

Example 5 — event.which + Block Enter

Legacy style still uses the same key codes — migrate the bind, keep the logic.

jQuery
var count = 0;

$("#target").keydown(function (event) {
  if (event.which === 13) {
    event.preventDefault();
  }
  count++;
  $("#log").text("Called " + count + " | key: " + event.which);
});

// Prefer: $("#target").on("keydown", function (event) { … });
Try It Yourself

How It Works

  1. Same which codes as the modern keydown event API.
  2. preventDefault on Enter (13) stops default form submit.
  3. Count climbs on every press — including OS hold-repeat.
  4. Swap the binder to .on("keydown", …) without changing the body.

Edge Cases & Pitfalls

Watch for these when reading or migrating .keydown().

deprecated

Do not use in new code

Binding with .keydown(fn) is deprecated since 3.3. Write .on("keydown", fn).

overload

Bind vs trigger look alike

.keydown(fn) binds; .keydown() triggers. Easy to confuse when skimming. Modern APIs make the intent obvious.

delegation

No selector form

.keydown() cannot delegate. Use $("#app").on("keydown", "input", fn) for dynamic fields.

event

Same keydown rules

Still needs focus; still may repeat while held. For release use keyup; for text values prefer the input event.

Browser Support

.keydown() has been available since jQuery 1.0 (eventData since 1.4.3) and is deprecated since jQuery 3.3. Prefer .on("keydown") / .trigger("keydown") everywhere jQuery runs.

✓ Deprecated · jQuery 3.3

jQuery .keydown()

Works in Chrome, Firefox, Safari, and Edge with jQuery 1.0+. Still present in jQuery 3.x for compatibility. New code should use .on('keydown') and .trigger('keydown').

Legacy Prefer .on / .trigger
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.keydown() Deprecated

Bottom line: Learn .keydown() to read legacy code. Write .on("keydown", fn) and .trigger("keydown") in new projects.

Performance Notes

ApproachCostBest for
.keydown(fn) / .on("keydown", fn)SameShorthand is a thin alias — no speed win
.on("keydown", "sel", fn)One listenerDynamic inputs (delegation)
Heavy work per pressNoticeableKeep handlers light — hold-repeat can spam

Migrating to .on does not change performance — it clarifies intent and unlocks delegation. Optimize the handler body, not the binding API.

Key Takeaways

  • Role: legacy bind/trigger shorthand for the keydown event.
  • Status: binding forms deprecated since jQuery 3.3.
  • Migrate: .on("keydown", fn) and .trigger("keydown").
  • Event: same press/hold-repeat rules as the keydown event.

One line: .keydown() is the old name — learn it for legacy code, write .on("keydown") / .trigger("keydown") for new work.

Frequently Asked Questions

With a handler — .keydown(handler) or .keydown(eventData, handler) — it binds a function that runs when the user presses a key on a focused element. With no arguments — .keydown() — it triggers the keydown event and runs bound handlers. Binding forms are deprecated since jQuery 3.3.
Yes. Since jQuery 3.3, .keydown(handler) and .keydown(eventData, handler) are deprecated. Use .on("keydown", handler) instead. Prefer .trigger("keydown") over no-argument .keydown().
.keydown(fn) registers a callback and does not fire immediately. .keydown() with no arguments triggers keydown on each matched element — same idea as .trigger("keydown").
Replace .keydown(fn) with .on("keydown", fn). Replace .keydown(data, fn) with .on("keydown", data, fn). Replace bare .keydown() with .trigger("keydown"). Under the hood the shorthand already uses .on — migration is a rename.
Yes. jQuery 3.x keeps it for compatibility. It still binds and triggers. The docs mark it deprecated so new projects use .on() and .trigger(). See the keydown event tutorial for the modern API.
Yes. keydown can repeat at the OS key-repeat rate while held. .keydown(handler) follows the same rule. keyup fires once on release.

Next: keypress event

Learn printable character input with the keypress event.

keypress tutorial →

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.

9 people found this page helpful