jQuery .keypress() Method

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

Definition

.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.

Syntax

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

jQuery
$("#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")
CallWhat it does
.keypress()Learn jQuery .keypress(): deprecated bind/trigger shorthand for keypress, migrate to .on('keypress') and .trigger('keypress'), eventData, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Find .keypress(fn) or bare .keypress() in legacy scripts.
Step 2Rewrite binds as .on("keypress", fn) (add eventData as the middle arg if needed).
Step 3Rewrite triggers as .trigger("keypress").

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 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.

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.

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.

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 — Key Release Handler

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

Keyup Event — Key Release Handler 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.

9 people found this page helpful