The keyup event fires when the user releases a key — once per press-and-release cycle, with no repeat while held. This tutorial covers .on("keyup"), event.which key codes, eventData, .trigger("keyup"), preventDefault for Enter, document-level shortcuts, and the official jQuery API demos.
01
.on()
Bind handler
02
event.which
Key code
03
.trigger()
Fire keyup
04
Focus
Target element
05
release
Key up
06
Since 1.7
Modern API
Fundamentals
Introduction
Sometimes you need to know when the user finished pressing a key — not when it first went down. The keyup event fires when a key comes back up, after the character has appeared in an input and after any keydown handlers have run.
The official jQuery API describes the keyupevent (bound with .on("keyup") since 1.7) separately from the deprecated .keyup()method. To programmatically run keyup handlers, use .trigger("keyup") since jQuery 1.0. For catching actual text entry as characters, keypress or the native input event may fit better — but keyup is ideal for reacting after each key release.
Concept
Understanding the keyup Event
The keyup event is sent to an element when the user releases a key on the keyboard. It fires once per key release — not repeatedly while the key is held down.
It can be attached to any element, but the event is only delivered to the element that has focus. Form elements — <input>, <textarea>, and <select> — are always reasonable targets. For page-wide shortcut keys, attach the handler to document so bubbling brings every key release to your code.
💡
Pair with keydown
Use keydown to detect when a key goes down (including repeats while held) and keyup to detect when it comes back up. On both events, examine event.which — jQuery normalizes key codes like 13 (Enter) and 27 (Escape) across browsers.
Foundation
📝 Syntax
The modern jQuery API for the keyup event has two main forms — binding a handler and triggering the event:
.on() and .trigger() return the original jQuery object for chaining.
Returning false from the handler can cancel default actions — same as preventDefault() + stopPropagation().
Cheat Sheet
⚡ Quick Reference
Goal
Code
Bind keyup handler
$("#target").on("keyup", fn)
Read key code
event.which — 13 Enter, 27 Escape
Pass data to handler
$("#field").on("keyup", { max: 10 }, fn)
React after key release
$("#field").on("keyup", fn) — runs once per keystroke
Global key release on page
$(document).on("keyup", fn)
Trigger keyup programmatically
$("#target").trigger("keyup")
Remove keyup handlers
$("#target").off("keyup")
Compare
📋 keyup vs keydown vs keypress vs deprecated .keyup()
Four related keyboard concepts — know which fires when, and what each is best for.
keydown event
key down
Every key press including arrows and modifiers — repeats while held; use event.which
keypress event
char
Mainly printable characters — narrower; prefer keydown or input event in new code
keyup event
key up
Fires when key is released — pair with keydown for hold detection
.keyup() method
deprecated
Deprecated shorthand for keyup — use .on("keyup") and .trigger("keyup") instead
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Examples 3–5 extend the official counter demo with keydown+keyup pairing, held-key tracking, and live updates on key release. Use the Try-it links to run each snippet in the browser.
📚 Binding & Triggering
Official jQuery demos for keyup handlers and programmatic triggers.
Example 1 — Official Demo: Bind Handler on #target
Alert when a key is pressed and released while the input has focus — official jQuery API demo.
Click inside #target, press and release a key → alert runs
Handler fires on keyup — when the key comes back up
Does NOT repeat while key is held (one keyup per release)
How It Works
.on("keyup", fn) runs when a key is released while #target has focus. Press and release a key inside the field — keyup does not fire on unfocused elements.
Example 2 — Official Demo: #other Triggers Keyup on #target
One button programmatically fires the keyup handler on the input.
Click Trigger the handler → trigger("keyup") → alert runs
Same handler as releasing a real key in #target
Useful for testing keyboard logic without simulating OS input
How It Works
.trigger("keyup") synthetically fires the event. Bound handlers run, but no actual key code is set unless you pass a simulated event object.
📈 Key Codes & Release Patterns
Official extended demo plus practical patterns for pairing keydown/keyup and reacting after release.
Example 3 — Official Demo: Counter, event.which, and Enter preventDefault
Count keyup events, block Enter from submitting, and log the event object — from the jQuery API documentation.
jQuery
var xTriggered = 0;
$( "#target" ).on( "keyup", function( event ) {
xTriggered++;
var msg = "Handler for `keyup` called " + xTriggered + " time(s).";
$( "#log" ).prepend( "
Each keyup increments counter and logs event.which
Enter blocked on keydown (13) — keyup still fires on release
Trigger button also increments counter via .trigger("keyup")
How It Works
The official API pairs keydown (to block Enter with preventDefault) and keyup (to count releases and log event.which). Enter prevention belongs on keydown; release detection belongs on keyup.
Example 4 — Pair keydown and keyup for Hold Detection
Track when a key goes down and when it comes back up — essential for games and modifier-key state.
Hold Space (32) → keydown repeats, keysDown tracks 32
Release Space → keyup fires once, key removed from keysDown
Pair both events for accurate "is key currently held?" state
How It Works
keydown adds the key code to a map; keyup removes it. This pattern avoids guessing from keydown repeats alone — the map always reflects keys currently held down.
Example 5 — Live Field Update on keyup
Update a character count or validation message after each key release — common in search and form fields.
jQuery
$( "#search" ).on( "keyup", function( event ) {
var len = $( this ).val().length;
var lastKey = event.which === 8 ? "Backspace" : String.fromCharCode( event.which );
$( "#status" ).text(
"Length: " + len + " | Last key released: " + lastKey
);
} );
Type "hello" → status updates after each key release
Value length reflects final input state (character already inserted)
For instant per-character updates, also consider the input event
How It Works
keyup runs after the browser inserts the character, so $(this).val() is up to date. This is a simple pattern for live counters; production search often debounces on input or keyup.
Applications
🚀 Common Use Cases
Post-keystroke validation — check field length or format after each key release on keyup.
Hold detection — pair keydown + keyup to track which keys are currently held.
Modifier release — detect when Shift or Ctrl is released via keyup on document.
Live character counts — update UI after each key release in text fields.
Live search — react on keyup after release (often paired with debounce).
Debugging — log event.which during development to learn key codes.
🚀 How the keyup Event Flows
1
User releases key
Physical key comes back up while an element (or document) has focus — or code calls .trigger("keyup").
release
2
keyup event fires
Handler runs on the focused element. Event object includes which, shiftKey, ctrlKey, and altKey.
keyup
3
Handler decides
Compare event.which, update UI, clear held-key state, or run validation after the keystroke completes.
which?
4
⌨
Bubble once
Event bubbles to ancestors (e.g. document). One keyup per press-and-release — no repeat while held.
Important
📝 Notes
Bind with .on("keyup", handler) since jQuery 1.7 — not the deprecated .keyup(handler) method.
Trigger with .trigger("keyup") since jQuery 1.0.
Event is sent only to the element with focus — click or tab into inputs first.
Use event.which for key codes — jQuery normalizes cross-browser differences.
keyup does not repeat while held — one event per key release.
For global shortcuts, bind on document — events bubble up from focused children.
For printable text input, consider the native input event — keyup fires after the character appears in the field.
Use .off("keyup") to remove handlers — avoid deprecated .unbind("keyup").
Compatibility
Browser Support
The keyup event is a standard DOM keyboard event supported in every browser jQuery targets. jQuery’s .on("keyup") (since 1.7) and .trigger("keyup") (since 1.0) normalize event.which for consistent key codes.
✓ jQuery 1.7+
jQuery keyup event
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery versions. Native equivalent: element.addEventListener('keyup', fn) — jQuery adds collection binding, eventData, delegation, and .trigger() in one chainable API.
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
keyupUniversal
Bottom line: Safe in any jQuery project. Prefer .on('keyup') over deprecated .keyup() for binding. Use event.which for key codes.
Wrap Up
Conclusion
The jQuery keyup event is the standard hook for reacting when keys are released — live validation, hold detection with keydown, and post-keystroke UI updates. Bind handlers with .on("keyup", fn), read event.which for key codes, and fire handlers programmatically with .trigger("keyup").
Remember: keyup requires focus on the target element unless you bind on document. Pair with keydown when you need both press and release. For dynamic pages, delegate with $("#app").on("keyup", "input", fn).
Bind with .on("keyup", handler) on inputs or document
Use event.which for reliable key codes across browsers
Pair keydown + keyup for accurate held-key tracking
Set tabindex="0" on custom focusable widgets
Check event.ctrlKey / shiftKey for modifier shortcuts
❌ Don’t
Use deprecated .keyup(handler) in new code — prefer .on("keyup")
Expect keyup on elements that never receive focus
Confuse keyup with keydown — keydown repeats while held, keyup does not
Use keyup alone for movement that should repeat while held — use keydown instead
Hard-code browser-specific key properties — use event.which
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about the keyup event
Release, code, pair.
6
Core concepts
.on01
.on("keyup")
Bind
API
which02
event.which
Key code
Detect
⚡03
.trigger()
Fire
Programmatic
focus04
Focus
Required
Target
doc05
document
Global
Shortcut
↩06
Repeats
While held
OS
❓ Frequently Asked Questions
The keyup event fires when the user releases a key on the keyboard. Unlike keydown, it does not repeat while a key is held — one keyup per press-and-release cycle. Bind handlers with .on('keyup', handler) since jQuery 1.7. The event is only sent to the element that has focus.
keydown fires when a key goes down and repeats while held (OS key repeat). keyup fires once when the key comes back up. Both report the same event.which key codes. Use keydown for shortcuts and movement; use keyup when you need to react after the user finishes pressing a key.
Read event.which — jQuery normalizes this property across browsers. Common codes: 13 Enter, 27 Escape, 32 Space, 37–40 arrow keys, 65–90 letter A–Z. The code reflects which key was released.
Call $('#target').trigger('keyup') since jQuery 1.0. Bound keyup handlers run. Note that .trigger() does not simulate a full physical key press and release — it only runs jQuery-bound handlers once per call.
Use keyup for validation after typing, live character counts, detecting when a modifier key is released, or any logic that should run once per keystroke after the character appears in the field. Use keydown for immediate shortcuts, games, and keys that repeat while held.
The target element must have focus when the key is released. Click inside an input first, or set tabindex on custom widgets. If you press a key outside the focused element, keyup goes to whatever has focus. For page-wide detection, bind on document.
Did you know?
jQuery’s $.event.special layer maps keyboard events consistently so event.which works even when older browsers exposed different properties (keyCode, charCode). That is why the official API recommends event.which instead of reading raw browser fields — one number for Enter, Escape, and arrow keys everywhere jQuery runs.