jQuery .keypress() Method

Beginner
⚠️ Deprecated since 3.3
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Keyboard events

What You’ll Learn

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

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.

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.

📝 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:

1. Bind a handler — .keypress( handler ) (since 1.0, deprecated 3.3)

jQuery
.keypress( handler )
  • handler — function executed each time keypress fires: function( event ) { ... }.
  • Returns the jQuery object for chaining.
  • Modern replacement: .on( "keypress", handler ).

2. Bind with eventData — .keypress( [eventData], handler ) (since 1.4.3, deprecated 3.3)

jQuery
.keypress( [eventData], handler )
  • eventData — optional object available in the handler as event.data.
  • Modern replacement: .on( "keypress", eventData, handler ).

3. Trigger the event — .keypress() (since 1.0)

jQuery
.keypress()
  • No arguments — triggers keypress on every matched element.
  • Runs bound keypress handlers.
  • Preferred replacement: .trigger( "keypress" ).

Official jQuery API migration note

jQuery
// Deprecated binding
$( "#target" ).keypress( function() {
  alert( "Handler for `keypress` called." );
} );

// Modern binding
$( "#target" ).on( "keypress", function() {
  alert( "Handler for `keypress` called." );
} );

// Deprecated trigger
$( "#target" ).keypress();

// Modern trigger
$( "#target" ).trigger( "keypress" );

Return value

  • All .keypress() signatures return the original jQuery object for chaining.
  • Handler return values are ignored unless you use return false (equivalent to preventDefault() + stopPropagation()).

⚡ Quick Reference

GoalLegacy (.keypress)Modern replacement
Bind keypress handler$("#target").keypress(fn)$("#target").on("keypress", fn)
Pass data to handler$("#search").keypress({ fieldId: "search" }, fn)$("#search").on("keypress", { fieldId: "search" }, fn)
Trigger keypress programmatically$("#target").keypress()$("#target").trigger("keypress")
Trigger on matched elements$("input").keypress()$("input").trigger("keypress")
Remove keypress handlers$("#target").off("keypress")$("#target").off("keypress")
Delegated keypress on inputs$("#app").on("keypress", "input", fn) — not available via .keypress()
event.which — character code: 97 lowercase a, 65 uppercase A, 13 Enter

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

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.

jQuery
$( "#target" ).keypress( function() {
  alert( "Handler for \`keypress\` called." );
} );
Try It Yourself

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.

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

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.

jQuery
var count = 0;

$( "#target" ).keypress( function( event ) {
  count++;
  $( "#out" ).text( "Target keypress handler fired (count: " + count + ")" );
} );

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

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." );
} );
Try It Yourself

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.

jQuery
$( "#target" )
  .keypress( function( event ) {
    if ( event.which === 13 ) {
      event.preventDefault();
    }
    $( "#notice" ).text( "First handler: Enter blocked." ).show();
  } )
  .keypress( function( event ) {
    $( "#result" ).text( "Second handler: char code " + event.which );
  } );
Try It Yourself

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

🚀 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").
  • Shared eventData$("#field").keypress({ id: "search" }, fn) passes metadata without closures.
  • 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").

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

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

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.

💡 Best Practices

✅ Do

  • Use .on("keypress", handler) for all new keypress bindings
  • Use .trigger("keypress") instead of no-arg .keypress()
  • Migrate .keypress(fn) to .on("keypress", fn) when refactoring legacy files
  • Use .off("keypress") to remove handlers cleanly
  • Read the modern keypress event tutorial for preventDefault and AJAX patterns

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about .keypress()

Legacy bind, modern migrate.

6
Core concepts
data 02

eventData

Since 1.4.3

Handler
() 03

.keypress()

Trigger

No args
.on 04

.on("keypress")

Modern

Replace
05

.trigger()

Fire

Programmatic
3.3 06

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.

Next: keyup event

Learn how keyup fires when keys are released — pair with keydown for hold detection.

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

6 people found this page helpful