JavaScript Element keyup Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM event
Instance event

What You’ll Learn

The keyup event fires when a key is released. Learn addEventListener vs onkeyup, key vs code, pairing with keydown, Tab focus caveats, IME tips, and five try-it labs.

01

Kind

Instance event

02

Type

KeyboardEvent

03

When

Key released

04

Handler

onkeyup

05

Pair

keydown

06

Status

Baseline widely available

Introduction

If keydown is “the key went down,” keyup is “the key came back up.” That release signal is perfect for stopping movement, ending a hold-to-fire action, or committing a shortcut only after the key lifts.

The event target is usually the focused element. If nothing suitable is focused, it may be Document or the root. The event bubbles to Document and Window.

💡
Beginner tip

For held keys, start on keydown and stop on keyup. For one-shot shortcuts (Save, Escape), keydown alone is often enough.

Understanding keyup

An instance event that answers: “Did a key just get released?”

  • Fires when any key is released.
  • KeyboardEventkey, code, modifier flags, isComposing.
  • Handleronkeyup or addEventListener("keyup", ...).
  • Bubbles — can reach Document / Window.
  • Pairs with keydown for press/release control.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("keyup", (event) => { });

onkeyup = (event) => { };

Event type

A KeyboardEvent (inherits from UIEvent and Event).

Handy properties

PropertyMeaning
keyKey value (e.g. "a", "Enter", "Escape")
codePhysical key id (e.g. "KeyA", "ArrowUp")
ctrlKey / shiftKey / altKey / metaKeyModifier flags active during the event
isComposingtrue during IME composition (between compositionstart/end)

⚠️ Tab Can Change the Target Before keyup

MDN notes that the event target can differ between keydown and keyup. With Tab, focus moves on the way down, so the release may fire on the newly focused element.

For IME composition, skip keyup when event.isComposing is true. Unlike keydown, there is no special keyCode === 229 marker on keyup.

JavaScript
eventTarget.addEventListener("keyup", (event) => {
  if (event.isComposing) {
    return;
  }
  // handle release safely
});

⚡ Quick Reference

GoalCode / note
Listenel.addEventListener("keyup", fn)
Handler propertyel.onkeyup = fn
Stop a hold actionClear flags / timers in keyup
Physical keyevent.code
IME-safeif (event.isComposing) return;
MDN statusBaseline Widely available

🔍 At a Glance

Four facts to remember about keyup.

Event type
KeyboardEvent

UIEvent + Event

Means
key released

End of the press

Pair
keydown

Start then stop

Baseline
yes

Widely available

Examples Gallery

Examples follow MDN Element: keyup event. Focus a field, press a key, then release it to see events.

📚 Getting Started

MDN listener patterns and the onkeyup property.

Example 1 — Log KeyboardEvent.code on Release (MDN)

Append each physical key code when the key comes up.

JavaScript
const input = document.querySelector("input");
const log = document.getElementById("log");

input.addEventListener("keyup", logKey);

function logKey(e) {
  log.textContent += " " + e.code;
}
Try It Yourself

How It Works

Nothing is logged until you release the key—that is the difference from keydown.

Example 2 — onkeyup Property

Show both key and code when the key is released.

JavaScript
const field = document.getElementById("field");
const out = document.getElementById("out");

field.onkeyup = (event) => {
  out.textContent = "released key=" + event.key + " code=" + event.code;
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Hold, Tab & IME

Pair with keydown, watch Tab focus shifts, and skip IME noise.

Example 3 — Hold to Move, Release to Stop

Start moving on keydown; clear the flag on keyup.

JavaScript
const stage = document.getElementById("stage");
const out = document.getElementById("out");
const held = new Set();

stage.tabIndex = 0;
stage.focus();

stage.addEventListener("keydown", (event) => {
  if (!event.key.startsWith("Arrow")) return;
  held.add(event.key);
  out.textContent = "holding: " + [...held].join(", ");
  event.preventDefault();
});

stage.addEventListener("keyup", (event) => {
  held.delete(event.key);
  out.textContent = held.size
    ? "holding: " + [...held].join(", ")
    : "released — idle";
});
Try It Yourself

How It Works

Tracking pressed keys in a Set and clearing them on keyup is a classic game-control pattern.

Example 4 — Tab Changes the keyup Target

Log which element receives keydown vs keyup for Tab.

JavaScript
const out = document.getElementById("out");

function label(el) {
  return el.id || el.tagName.toLowerCase();
}

document.addEventListener("keydown", (event) => {
  if (event.key !== "Tab") return;
  out.textContent = "keydown on " + label(event.target);
});

document.addEventListener("keyup", (event) => {
  if (event.key !== "Tab") return;
  out.textContent += " → keyup on " + label(event.target);
});
Try It Yourself

How It Works

Focus moves before the key is released, so the two events can report different targets—exactly as MDN describes.

Example 5 — IME-Safe Release Handler

Ignore composition keyup events before handling a release action.

JavaScript
const out = document.getElementById("out");

document.addEventListener("keyup", (event) => {
  if (event.isComposing) {
    return;
  }
  if (event.key === "Enter") {
    out.textContent = "Enter released (not composing)";
  }
});
Try It Yourself

How It Works

MDN’s keyup IME tip only checks isComposing—no keyCode === 229 special case.

🚀 Common Use Cases

  • Stopping movement or shooting when a key is released.
  • Clearing “held” shortcut state after Ctrl/Cmd combinations.
  • Committing an action only after the key lifts (less accidental repeats).
  • Accessibility patterns that need press and release symmetry.
  • Debugging focus changes with Tab by comparing keydown/keyup targets.

🔧 How It Works

1

keydown

Key goes down; optional repeats while held.

Press
2

Your action runs

Start moving, open a menu, or arm a hold state.

Active
3

Key lifts

keyup fires with key / code.

Release
4

Cleanup

Stop loops, clear flags, and restore idle UI.

📝 Notes

  • MDN: Baseline Widely available — no Deprecated / Experimental / Non-standard banner.
  • Prefer keydown / keyup over deprecated keypress.
  • Tab may change focus between keydown and keyup.
  • IME guard for keyup: check isComposing (no keyCode === 229 tip).
  • Related learning: keydown, keypress (deprecated), addEventListener(), JavaScript hub.

Browser Support

keyup is marked Baseline Widely available on MDN. Logos use the shared browser-image-sprite.png sprite from this project. KeyboardEvent key/code are the modern path across engines.

Baseline · Widely available

Element keyup

Fires when a key is released; pair with keydown for press/release control.

Full Widely available
Google Chrome Full support
Yes
Mozilla Firefox Full support
Yes
Apple Safari Full support
Yes
Microsoft Edge Full support
Yes
Opera Full support
Yes
Internet Explorer Supported (prefer feature-detect modern key/code)
Partial
keyup Baseline

Bottom line: Safe default for release handling. Watch Tab target changes and skip IME composition keyups.

Conclusion

keyup completes the keyboard story: detect the release, clean up held state, and pair it with keydown for full press/release control.

Continue with lostpointercapture, keydown, keypress (deprecated), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pair keydown start actions with keyup cleanup
  • Use event.key / event.code for modern detection
  • Skip IME noise with event.isComposing
  • Account for Tab changing the focused target before release
  • Prefer addEventListener in production code

❌ Don’t

  • Rely on deprecated keypress for release logic
  • Assume keydown and keyup always share the same target
  • Forget to clear held-key sets when the window loses focus
  • Ignore IME composition in international text apps
  • Overwrite onkeyup if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about keyup

Key released—stop holds and finish the keyboard pair.

5
Core concepts
📄 02

KeyboardEvent

key · code · mods

API
03

Pairs with keydown

start / stop

Pattern
🔄 04

Tab caveat

target may change

Focus
🎯 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It fires when a key is released. Together with keydown it gives you a press/release pair for shortcuts, games, and held-key controls.
No. MDN marks Element keyup as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
A KeyboardEvent (inherits from UIEvent and Event). Useful properties include key, code, altKey, ctrlKey, shiftKey, metaKey, and isComposing.
keydown fires when the key goes down (and may repeat while held). keyup fires once when the key is released. Start actions on keydown; stop or commit on keyup.
Yes. For Tab, keydown and keyup can target different elements because focus already moved. Always check event.target when that matters.
Skip events when event.isComposing is true. Unlike keydown, keyup does not use the special keyCode 229 IME marker.
Did you know?

If the user Alt-Tabs away while holding a key, you may miss keyup. Many games also clear held-key state on blur / visibilitychange so movement does not stick.

Next: lostpointercapture

Learn when pointer capture ends and how to clean up drag UI.

lostpointercapture →

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.

5 people found this page helpful