jQuery Keypress Event

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

Definition

The keypress event fires when the browser registers printable character input. Unlike keydown, modifiers and non-printing keys (Shift, Esc, Delete) usually do not trigger it. Bind with .on("keypress") (since jQuery 1.7); the event reaches only the focused element.

Syntax

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

jQuery
$("#target").on("keypress", handler)   // bind (1.7+)
$("#target").trigger("keypress")       // fire handlers (1.0+)

event.which → character code
  "a" → 97    "A" → 65    Enter → 13

arrows / Shift / Esc → usually NO keypress
(use keydown instead)
CallWhat it does
KeypressLearn the jQuery keypress event: bind with .on('keypress'), read character codes via event.which, trigger handlers, compare keypress vs keydown, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Pick a focusable target — usually an <input> or <textarea>.
Step 2Bind with $("#target").on("keypress", handler) (jQuery 1.7+).
Step 3Inside the handler, read event.which for the character code.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Bind a Keypress Handler

Log when a printable key is pressed while the input has focus.

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

How It Works

  1. Select the input. $("#target") must be able to receive focus.
  2. Bind keypress. .on("keypress", fn) registers the handler (jQuery 1.7+).
  3. Type a letter. Printable input fires the handler; the message logs to the console.

Example 2 — Trigger Keypress from a Button

Fire the bound handler programmatically with .trigger("keypress").

jQuery
$("#target").on("keypress", function () {
  console.log("Handler for keypress called.");
});

$("#other").on("click", function () {
  $("#target").trigger("keypress");
});
Try It Yourself

How It Works

  1. Bind first. Same handler as Example 1 on #target.
  2. Click the button. The click handler runs $("#target").trigger("keypress").
  3. Handlers fire once. Bound keypress listeners run; this is not a full OS key simulation.

Example 3 — Character Code and Block Enter

Count presses, log event.which, and stop Enter with preventDefault.

jQuery
var count = 0;

$("#target").on("keypress", function (event) {
  if (event.which === 13) {
    event.preventDefault();
  }
  count++;
  $("#log").text(
    "Called " + count + " time(s). Char: " + event.which
  );
});
Try It Yourself

How It Works

  1. Read event.which. On keypress this is the character code.
  2. Block Enter. Code 13 → preventDefault() stops the default action.
  3. Count each fire. Every printable key increments count and updates the log.

Frequently Asked Questions

keypress fires when the browser registers keyboard input that produces a character. Modifier and non-printing keys such as Shift, Escape, and Delete trigger keydown but usually not keypress. Bind with .on('keypress', handler) since jQuery 1.7. The event reaches only the focused element.
keydown fires for every key, including arrows and modifiers. keypress is aimed at printable character input. keydown reports the key (lowercase a = 65); keypress reports the character (lowercase a = 97). For arrows and shortcuts, prefer keydown.
Use event.which — jQuery normalizes it across browsers. On keypress it is the character code: lowercase a is 97, uppercase A is 65, Enter is 13. String.fromCharCode(event.which) gives the typed character when appropriate.
keypress is not covered by any official DOM specification, so behavior can vary. Many modern apps prefer the input event for text fields or keydown for all keys. Keep keypress for legacy jQuery code, and know its limits.
Call $('#target').trigger('keypress') since jQuery 1.0. Bound handlers run. .trigger() does not fully simulate a real typed character unless you pass a simulated event with the correct which value.
Arrows, Shift, Ctrl, Escape, and other non-printing keys do not produce character input, so browsers typically skip keypress. Use .on('keydown') and check event.which (37–40 for arrows) when you need those keys.

Next: .keypress() Method — Deprecated Event Shorthand

Learn the deprecated shorthand and how to migrate to .on("keypress").

.keypress() Method — Deprecated Event Shorthand 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.

10 people found this page helpful