HTML onkeyup Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onkeyup attribute is an inline event handler that runs JavaScript when the keyup event fires — when the user releases a key on the keyboard. Attach it to focusable elements like <input> and <textarea>, or listen on document with addEventListener. It fires after keydown for the same key press. Common uses include simple live filters, reacting when Enter is released, and game controls. For value-based live search, oninput is often a better fit; use onkeyup when you specifically need the moment of key release.

What You’ll Learn

01

Event handler

Inline JS.

02

keyup event

Key released.

03

event.key

Which key.

04

vs onkeydown

Up vs down.

05

addEventListener

Preferred way.

06

Live search

Filter lists.

Purpose of onkeyup Attribute

The primary purpose of onkeyup is to run code when a key finishes its press — after the user lifts their finger. That timing can matter for shortcuts, toggles, or reading the final character in the field after it was added. Many tutorials use it for live search; reading input.value inside the handler updates results as the user types.

Because keyup fires for every key release (including Shift, Ctrl, and arrows), check event.key when you only care about certain keys. Debounce or throttle handlers if you call a server on every keyup to avoid flooding the network.

💡
oninput vs onkeyup for search

oninput fires when the value changes (including paste). onkeyup fires on key release even if the value did not change. For filtering a list by text, oninput is usually simpler.

📝 Syntax

Set onkeyup on a focusable element, or assign element.onkeyup:

onkeyup.html
<input type="text" onkeyup="handleKeyUp(event)">

<script>
  function handleKeyUp(e) {
    console.log("Key released:", e.key);
  }
</script>

Syntax Rules

  • Value is JavaScript executed when the keyup event fires.
  • Works on focusable elements and via document.addEventListener("keyup", …).
  • Fires after keydown for the same physical press.
  • Pass event to read key, code, and modifier flags.
  • Does not fire for paste-only value changes — use oninput for that.
  • Modern alternative: element.addEventListener("keyup", handler).
  • The keyup event bubbles.

💎 Values

The onkeyup attribute accepts a string of JavaScript code:

  • onkeyup="handleKeyUp()" — Call a named function.
  • onkeyup="searchFunction()" — Run search after key release.
  • JavaScript: field.onkeyup = (e) => { … } assigns the handler.
onkeyup-js.html
searchInput.addEventListener("keyup", (e) => {
  if (e.key === "Enter") {
    runSearch(searchInput.value);
    return;
  }
  filterResults(searchInput.value);
});

⚡ Quick Reference

Actionkeyup fires?Notes
Release letter key in inputYesAfter keydown
Release EnterYesCommon submit trigger
Release EscapeYesClose/clear patterns
Paste text (no key up in field)NoUse oninput
Key still held downNoWaits for release
Handler attributeonkeyupInline on element

Applicable Elements

TargetSupported?Notes
<input>, <textarea>YesWhen focused
<button>, <select>YesKeyboard activation
tabindex="0" on divYesCustom keyboard targets
document / windowYesVia addEventListener
<div> without tabindexNoNot focusable

onkeyup vs onkeydown vs oninput

HandlerWhen it firesTypical use
onkeydownKey pressed downShortcuts, prevent default early
onkeyupKey releasedAfter keystroke, Enter handling
oninputValue changedLive search, character count

Examples Gallery

Live filter on key release, addEventListener, and Enter to run search.

👀 Live Preview

Type in the search box — results filter each time you release a key:

  • Apple
  • Banana
  • Orange
  • Grape

Dynamic Values with JavaScript

Attach the handler with addEventListener:

dynamic-onkeyup.html
<input type="text" id="dynamicInput">

<script>
  document.getElementById("dynamicInput").addEventListener("keyup", function (e) {
    document.getElementById("log").textContent = "Key released: " + e.key;
  });
</script>
Try It Yourself

How It Works

Register once at page load. Every key release while the field is focused triggers the callback.

Example — Enter on keyup

Run search when the user releases Enter:

enter-onkeyup.html
searchInput.addEventListener("keyup", (e) => {
  if (e.key === "Enter") {
    runSearch(searchInput.value.trim());
  }
});
Try It Yourself

How It Works

Filter on every keyup, but only call heavy runSearch when Enter is released — a basic debounce-style split.

♿ Accessibility

  • Do not block Tab — Avoid preventDefault() on Tab in keyup handlers.
  • Announce live results — Use aria-live="polite" on search result regions.
  • Prefer native forms — Submit with a <button type="submit"> for keyboard users.
  • Debounce API calls — Rapid keyups should not overwhelm users with flickering content.
  • Document shortcuts — Tell users if Enter or Escape do something special.

🧠 How onkeyup Works

1

User presses key

keydown fires first.

keydown
2

User releases key

Physical key up.

Release
3

keyup fires

onkeyup handler runs.

Event
=

React after keystroke

Search, Enter, feedback.

Browser Support

The keyup event and onkeyup handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge.

DOM Events · Fully supported

Universal onkeyup support

All major browsers fire keyup and honor the onkeyup handler on focusable elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onkeyup attribute 99% supported

Bottom line: Reliable for keyboard release handling; pair with oninput when value changes matter more than key timing.

💡 Best Practices

✅ Do

  • Use event.key to detect Enter, Escape, or specific keys
  • Prefer addEventListener("keyup", …) in production
  • Debounce or throttle expensive search/API calls
  • Use oninput when paste and autofill must trigger updates
  • Pair with onkeydown when you need both press and release

❌ Don’t

  • Rely on keyup alone for all live search — consider oninput
  • Block Tab or standard browser shortcuts without good reason
  • Fire unbounded server requests on every keyup
  • Use deprecated keyCode in new code
  • Attach onkeyup to non-focusable elements without tabindex

Conclusion

The onkeyup attribute runs JavaScript when a key is released — useful for live filters, Enter handling, and keyboard-driven interactions after each keystroke completes.

Prefer addEventListener("keyup", …), use event.key, and choose oninput when the field value matters more than key timing.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onkeyup

Bookmark these before adding keyboard release handlers.

5
Core concepts
📝 02

event.key

Enter, Escape.

API
🔄 03

vs oninput

Value changes.

Compare
🔍 04

Live filter

Read .value.

Pattern
05

Debounce

API calls.

Perf

❓ Frequently Asked Questions

It runs JavaScript when the keyup event fires — when the user releases a key while the element is focused.
onkeydown = key pressed down. onkeyup = key released. keydown always fires first.
Prefer oninput for filtering by text — it handles paste too. onkeyup still works for simple demos.
Not necessarily — paste updates the value without a key release in the field. Use oninput for paste.
addEventListener("keyup", handler) is preferred for production. Inline onkeyup works for learning.
Yes in all modern browsers; Internet Explorer 9+.

Master key release events

Practice the onkeyup attribute with live filter and Enter examples in the Try It editor.

Try onkeyup demo →

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