jQuery .keypress() Method

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

What Is .keypress()?

.keypress() is jQuery’s legacy shorthand for the keypress 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").keypress(fn)           // bind (deprecated)
$("#el").keypress(data, fn)     // bind + eventData (deprecated)
$("#el").keypress()             // trigger handlers

// Modern replacements:
$("#el").on("keypress", fn)
$("#el").trigger("keypress")
NeedPrefer
Bind keypress.on("keypress", fn)
Trigger keypress.trigger("keypress")
Read legacy code.keypress(fn) / .keypress()
All keys / arrows.on("keydown", fn)

How to Use .keypress()

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

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

Pseudocode

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

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

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

Cheat Sheet

Legacy vs modern — keep this table for quick migrations.

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

Live Preview

Compare the deprecated shorthand with the modern API. Type in the field, or click Trigger to fire handlers.

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

Result
$("#field").keypress(fn)  // deprecated bind
→ type a letter to fire

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

jQuery Examples

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

Example 1 — Bind with .keypress(handler)

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

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

How It Works

  1. Select the input. $("#target") must be focusable.
  2. Call the shorthand. .keypress(fn) registers the handler (internally uses .on).
  3. Type a character. Printable input fires the handler; the message logs.
  4. Prefer modern syntax. New code should use .on("keypress", fn).

Example 2 — Pass eventData

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

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

How It Works

  1. Pass data first. { fieldId: "search" } is stored with the handler.
  2. Read event.data. On keypress, the object is available without a closure.
  3. Use event.which. Still the character code on keypress.
  4. Migrate. Same pattern with .on("keypress", data, fn).

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

Calling .keypress() without a handler fires bound keypress listeners once.

jQuery
var count = 0;

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

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

How It Works

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

Example 4 — Side-by-Side Migration

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

jQuery
// Legacy
$("#target").keypress(function () {
  console.log("legacy");
});
$("#target").keypress();

// Modern (prefer this)
$("#target").on("keypress", function () {
  console.log("modern");
});
$("#target").trigger("keypress");
Try It Yourself

How It Works

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

Edge Cases & Pitfalls

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

deprecated

Do not use in new code

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

overload

Bind vs trigger look alike

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

delegation

No selector form

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

event

Same keypress limits

Still needs focus; still skips arrows/modifiers. For all keys use keydown; for text values prefer the input event.

Browser Support

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

✓ Deprecated · jQuery 3.3

jQuery .keypress()

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

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
.keypress() Deprecated

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

Performance Notes

ApproachCostBest for
.keypress(fn) / .on("keypress", fn)SameShorthand is a thin alias — no speed win
.on("keypress", "sel", fn)One listenerDynamic inputs (delegation)
Heavy work per keyNoticeableKeep handlers light; debounce search

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 keypress event.
  • Status: binding forms deprecated since jQuery 3.3.
  • Migrate: .on("keypress", fn) and .trigger("keypress").
  • Event: same printable-character rules as the keypress event.

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

Frequently Asked Questions

With a handler — .keypress(handler) or .keypress(eventData, handler) — it binds a function for printable keyboard input on a focused element. With no arguments — .keypress() — it triggers the keypress event and runs bound handlers. Binding forms are deprecated since jQuery 3.3.
Yes. Since jQuery 3.3, .keypress(handler) and .keypress(eventData, handler) are deprecated. Use .on('keypress', handler) instead. Prefer .trigger('keypress') over no-argument .keypress().
.keypress(fn) registers a callback and does not fire immediately. .keypress() with no arguments triggers keypress on each matched element — same idea as .trigger('keypress').
Replace .keypress(fn) with .on('keypress', fn). Replace .keypress(data, fn) with .on('keypress', data, fn). Replace bare .keypress() with .trigger('keypress'). 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 keypress event tutorial for the modern API.
Arrows, Shift, Ctrl, and Escape are non-printing keys, so browsers usually skip keypress. Use .on('keydown', fn) when you need those keys.

Next: keyup event

Learn how to handle key release with .on("keyup") — pairs well with keydown.

keyup 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