The .keypress() method is jQuery’s legacy shorthand for binding and triggering the keypress event. This tutorial covers .keypress(handler) since 1.0, .keypress(eventData, handler) since 1.4.3, no-argument .keypress() as a trigger, migration to .on("keypress") and .trigger("keypress"), and why the binding form is deprecated since jQuery 3.3. See the jQuery keypress event tutorial for the modern API.
01
.keypress(fn)
Bind handler
02
eventData
Since 1.4.3
03
.keypress()
Trigger event
04
.on()
Modern bind
05
.trigger()
Modern fire
06
Deprecated
Since 3.3
Fundamentals
Introduction
Before jQuery 1.7 unified events with .on() and .off(), every common event had a matching shorthand — .keypress(), .keyup(), .click(), and more. The .keypress() method let you react to typed characters in one short call: $("#phone").keypress(function(e){ if(e.which<48||e.which>57){ e.preventDefault(); } }). It remains in jQuery 3.x for backward compatibility but is marked deprecated.
Understanding .keypress() matters when reading older form validation and numeric-field tutorials in legacy codebases. The method does two distinct jobs: with a function argument it binds; with no arguments it triggers. Modern jQuery splits those roles explicitly — .on("keypress", handler) for binding and .trigger("keypress") for firing.
⚠️
Focus required
The jQuery .keypress() method attaches to any element, but the browser only delivers keypress events to the element that has focus. Click inside an input or set tabindex="0" on custom widgets before expecting handlers to run. Arrow keys and Shift do not fire keypress — use $(document).on("keydown", fn) for shortcuts. keypress only fires for printable character input.
Concept
Understanding the .keypress() Method
The official jQuery API describes .keypress() as a way to bind an event handler to the keypress event, or trigger that event on an element. When you pass a handler, jQuery registers it on each matched element and calls it when the user presses a key while that element has focus — the same keypress event that .on("keypress") handles today.
When you call .keypress() with no arguments, jQuery synthetically fires the keypress event on each matched element. Bound handlers run once per call. This trigger behavior is equivalent to .trigger("keypress").
⚠️
Deprecated since jQuery 3.3
Do not use .keypress(handler) or .keypress(eventData, handler) in new code. Replace them with .on("keypress", handler) or .on("keypress", eventData, handler). Replace no-argument .keypress() with .trigger("keypress"). For event.which character codes, digit filters, and the full modern keypress guide, read the jQuery keypress event tutorial.
Foundation
📝 Syntax
The .keypress() method has three signatures from the official jQuery API. The binding forms are deprecated; the trigger form remains valid but .trigger("keypress") is preferred:
$("#app").on("keypress", "input", fn) — not available via .keypress()
event.which — character code: 97 lowercase a, 65 uppercase A, 13 Enter
Compare
📋 .keypress() vs .on("keypress") vs .trigger("keypress") vs .off("keypress")
Four related APIs — know which one binds, which one fires, and which one removes handlers.
.keypress()
deprecated
Legacy bind (.keypress(fn)) or trigger (.keypress()) — use modern APIs for new code
.on("keypress")
bind
Modern binding since 1.7 — supports eventData and delegation on dynamic inputs
.trigger("keypress")
fire
Programmatically run handlers — clearer than no-arg .keypress()
.off("keypress")
remove
Unbind handlers — pair with .on("keypress") when cleaning up or in SPAs
Hands-On
Examples Gallery
Five examples cover binding, eventData, triggering, migration from .keypress() to .on("keypress"), and method chaining. Use the Try-it links to run each snippet in the browser. Remember: binding with .keypress(handler) is deprecated — these demos show legacy syntax you will encounter in older code.
📚 Binding & Triggering
Legacy .keypress() signatures for binding handlers and firing events programmatically.
Example 1 — Bind Handler with .keypress(handler)
The canonical legacy pattern — alert when a printable character is typed while the input has focus.
Focus #target, type a letter or number → alert runs
Arrow keys do NOT fire keypress
Deprecated — prefer .on("keypress", fn) for new code
How It Works
.keypress(fn) registers the function on the element. jQuery internally routes this to .on("keypress", fn). The handler runs when printable input is registered while the element has focus.
Example 2 — Pass eventData with .keypress(eventData, handler)
Since jQuery 1.4.3, pass field metadata before the handler — it arrives as event.data.
Focus #search, press a key → "Field: search | char code: 97" (for a)
eventData.fieldId passed without a closure
Modern: .on("keypress", { fieldId: "search" }, fn)
How It Works
jQuery stores the { fieldId: "search" } object and injects it into event.data when keypress fires. Modern equivalent: .on("keypress", { fieldId: "search" }, fn).
Example 3 — Trigger Keypress with No-Argument .keypress()
Calling .keypress() without a handler fires the keypress event programmatically — bound handlers run once per call.
Press a key in #target → count increments
Click #fire → $("#target").keypress() → same handler runs
No-arg .keypress() is trigger shorthand — prefer .trigger("keypress")
How It Works
The first .keypress(function(){ ... }) binds a handler on #target. The button calls $("#target").keypress() with no arguments — that synthetically fires keypress. Replace with $("#target").trigger("keypress") for clarity.
📈 Migration & Chaining
Compare legacy and modern APIs, and chain after .keypress(handler).
Example 4 — Migration: .keypress(fn) vs .on("keypress", fn)
Both bind the same keypress event — .on() is the recommended API for new code.
jQuery
$( "#legacy" ).keypress( function() {
$( "#out" ).text( "Legacy: .keypress(function(){ ... }) — deprecated since jQuery 3.3." );
} );
$( "#modern" ).on( "keypress", function() {
$( "#out" ).text( "Modern: .on('keypress', function(){ ... }) — use this for new code." );
} );
Focus #legacy, press a key → legacy deprecation message
Focus #modern, press a key → modern .on("keypress") message
Behavior is identical — only the API name differs
How It Works
Under the hood, .keypress(fn) calls .on("keypress", fn). Migration is a straight rename with no behavior change for simple bindings. The advantage of .on() is delegation — $("#app").on("keypress", "input", fn) — which .keypress() cannot do.
Example 5 — Chaining After .keypress(handler)
.keypress(handler) returns the jQuery object — chain multiple keyboard handlers on the same input.
Press any key in #target
First handler shows #notice; Enter gets preventDefault
Second handler updates #result with event.which
Both handlers run in bind order on each keypress
How It Works
Multiple .keypress(fn) calls on the same element stack handlers — all run when a key is pressed. Because each call returns the jQuery object, you can chain them fluently. The same pattern works with .on("keypress", fn).
Applications
🚀 Common Use Cases
Reading legacy code — recognize $("#search").keypress(fn) as equivalent to .on("keypress", fn) in older scripts.
Character filtering — legacy numeric fields bind .keypress() to allow digits only with event.which.
Programmatic trigger — $("#target").keypress() from a test button — replace with .trigger("keypress").
Live character preview — chained .keypress(fn) calls show typed characters with String.fromCharCode(event.which).
Maintaining legacy form validation — many jQuery 1.x forms still use .keypress() — know how to migrate safely.
🧠 How .keypress() Routes Through jQuery
1
You call .keypress()
With a function argument, jQuery treats it as a bind request. With no arguments, it treats it as a trigger request.
dispatch
2
Bind path
.keypress(handler) delegates internally to .on("keypress", handler) — the handler is stored in jQuery’s event system.
.on("keypress")
3
Trigger path
No-arg .keypress() delegates to .trigger("keypress") — bound handlers run when keypress fires.
.trigger("keypress")
4
✎
jQuery object returned
Both paths return the original collection for chaining — .keypress(fn).addClass("watched") works the same as .on("keypress", fn).addClass("watched").
Important
📝 Notes
Deprecated since jQuery 3.3 — do not use .keypress(handler) or .keypress(eventData, handler) in new code. Use .on("keypress") instead.
Bind with .keypress(handler) since jQuery 1.0; eventData form since 1.4.3.
No-argument .keypress() triggers the event since jQuery 1.0 — prefer .trigger("keypress").
.keypress() does not support event delegation — use .on("keypress", "input", fn) for dynamic forms.
Keyboard events require focus on the target element — click or tab into inputs first.
Read character codes with event.which inside handlers — on keypress this is the typed character, not the physical key.
Remove handlers with .off("keypress") — not by calling .keypress(undefined).
For the full modern keypress guide — event.which character codes, digit filters, and Enter handling — see the jQuery keypress event tutorial.
Compatibility
Browser Support
The underlying keypress event fires on focused elements in browsers jQuery targets. The .keypress() method itself is a jQuery API — it works in jQuery 1.x, 2.x, and 3.x. Binding via .keypress(handler) is deprecated since 3.3 but still functional; triggering via no-arg .keypress() also remains supported.
✓ jQuery 1.0+
jQuery .keypress() method
Supported across all jQuery versions you are likely to maintain. Deprecated binding since 3.3 — use .on('keypress') for new projects. Native equivalent for binding: element.addEventListener('keypress', fn). Native equivalent for trigger: .trigger('keypress') or dispatchEvent.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.keypress()Legacy API
Bottom line: Safe in legacy jQuery projects. Migrate bindings to .on('keypress') and triggers to .trigger('keypress') when updating legacy form validation and character-filter code.
Wrap Up
Conclusion
The jQuery .keypress() method is the legacy shorthand for binding and triggering keypress handlers. Use .keypress(handler) or .keypress(eventData, handler) to bind — both deprecated since jQuery 3.3. Use no-argument .keypress() to trigger — equivalent to .trigger("keypress").
For new code, prefer .on("keypress", fn) for binding and .trigger("keypress") for programmatic firing. When you encounter .keypress() in older form validation and numeric-field scripts, recognize it, understand it, and migrate when practical. For event.which character codes, digit filters, and the complete modern keypress workflow, continue with the jQuery keypress event tutorial.
Write new keyboard handlers with deprecated .keypress(handler)
Expect keypress only on focused elements — arrow keys need .on("keydown")
Assume .keypress() supports delegation — it does not
Confuse .keypress(fn) (bind) with .keypress() (trigger)
Use .unbind("keypress") — it is also deprecated; use .off("keypress")
Copy deprecated patterns from old tutorials without modernizing them
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .keypress()
Legacy bind, modern migrate.
6
Core concepts
.kp01
.keypress(fn)
Bind
Deprecated
data02
eventData
Since 1.4.3
Handler
()03
.keypress()
Trigger
No args
.on04
.on("keypress")
Modern
Replace
⚡05
.trigger()
Fire
Programmatic
3.306
Deprecated
Since 3.3
Migrate
❓ Frequently Asked Questions
The .keypress() method has two roles. With a handler argument — .keypress(handler) or .keypress(eventData, handler) — it binds a function that runs when printable keyboard input is registered on a matched element that has focus. With no arguments — .keypress() — it triggers the keypress event on each matched element, running bound handlers. Both forms have been part of jQuery since 1.0; the binding form is deprecated since 3.3.
Yes — the binding signatures .keypress(handler) and .keypress(eventData, handler) are deprecated since jQuery 3.3. Use .on('keypress', handler) or .on('keypress', eventData, handler) instead. The no-argument .keypress() trigger form still works but .trigger('keypress') is clearer and preferred in modern code.
.keypress(handler) registers a callback — it does not fire the event immediately. .keypress() with no arguments triggers the keypress event programmatically on every matched element, the same as .trigger('keypress'). The handler form is deprecated for binding; the no-arg form is a trigger shorthand.
Replace .keypress(fn) with .on('keypress', fn). Replace .keypress(data, fn) with .on('keypress', data, fn). Replace no-arg .keypress() with .trigger('keypress'). Replace .unbind('keypress') cleanup with .off('keypress'). Under the hood, .keypress(handler) already delegates to .on('keypress') — migration is a straight rename.
Yes. jQuery 3.x keeps .keypress() for backward compatibility. It still binds and triggers correctly. The API docs mark it deprecated to steer new projects toward .on() and .trigger(). Do not use .keypress(handler) in new code — prefer the modern API in the keypress event tutorial.
Arrow keys, Shift, Ctrl, and Escape do not produce printable character input, so browsers typically skip keypress for them. .keypress(handler) only runs for characters the browser registers as typed input. Use .on('keydown', fn) when you need non-printing keys.
Did you know?
jQuery event shorthands like .keypress(handler) were thin wrappers around .bind("keypress", handler) before .on() unified the API in jQuery 1.7. When you call .keypress(fn) today, jQuery still routes through the same event system as .on("keypress", fn) — which is why migration is a rename with no behavior change for simple bindings.