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)
Call
What it does
Keypress
Learn 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
Parameter
Description
Step 1
Pick a focusable target — usually an <input> or <textarea>.
Step 2
Bind with $("#target").on("keypress", handler) (jQuery 1.7+).
Step 3
Inside the handler, read event.which for the character code.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters 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.");
});
Focus #target, type a letter →
Handler for keypress called.
(Arrow keys / Shift do not fire keypress)
How It Works
Select the input.$("#target") must be able to receive focus.
Bind keypress..on("keypress", fn) registers the handler (jQuery 1.7+).
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");
});
Type "a" → Called 1 time(s). Char: 97
Type "A" → Called 2 time(s). Char: 65
Press Enter → preventDefault (no submit)
How It Works
Read event.which. On keypress this is the character code.
Block Enter. Code 13 → preventDefault() stops the default action.
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.