HTML onkeyup Attribute

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onkeyup attribute is an inline event handler for the keyup event. It runs when the user releases a key on a focused element — for example an <input>, <textarea>, or any focusable control (see tabindex).

Unlike deprecated onkeypress, keyup fires for Escape, arrows, and most other keys. Compare with onkeydown (press) and oninput (value changed).

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

HTML Syntax

syntax.html
<element onkeyup=" script">

script is JavaScript that runs when the key is released. Pass event to read event.key or event.code. Script alternative: element.addEventListener("keyup", handler).

Attribute Values

The attribute value is a script. Common patterns:

ValueDescriptionTry it
handler()Call a named function when any key is releasedTry it
handler(event)Pass the KeyboardEvent to read event.key / event.codeTry it
Live filterUpdate a list or message after each key release in a search fieldTry it
Detect a keyReact to Escape, Enter, or another named key on releaseTry it

Live Preview

Focus the field, hold a key, then release it. The log updates on keyup (not while the key is held down).

mode

Release any key to see event.key and event.code.

Input

Last keyup: (none)

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Log Released Keys

Show which key was released using event.key and event.code.

HTML
<label for="sample">Press and release a key:</label>
<input type="text" id="sample" onkeyup="logKey(event)">
<p id="log">Waiting…</p>

<script>
  function logKey(e) {
    document.getElementById("log").textContent =
      "Released: " + e.key + " (" + e.code + ")";
  }
</script>
Try It Yourself

How It Works

  1. Focus the input. Keyboard events go to the focused element.
  2. Press, then release. keyup fires only on release (not while held).
  3. Read event.key. Human-readable name or character (e.g. a, Escape).
  4. Optional event.code. Physical key id (e.g. KeyA) — layout-independent.

Example 2 — Live List Filter

Update a short list after each key release in a search field.

HTML
<label for="q">Filter fruits:</label>
<input type="search" id="q" onkeyup="filterList()">
<ul id="list">
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
  <li>Date</li>
</ul>

<script>
  function filterList() {
    var q = document.getElementById("q").value.toLowerCase();
    var items = document.querySelectorAll("#list li");
    for (var i = 0; i < items.length; i++) {
      var text = items[i].textContent.toLowerCase();
      items[i].hidden = q !== "" && text.indexOf(q) === -1;
    }
  }
</script>
Try It Yourself

How It Works

  1. Type in the search field. Each release runs filterList().
  2. Read the current value. By keyup time, the character is already in the input.
  3. Hide non-matches. hidden toggles list items without removing them.
  4. Note: For paste-aware filtering, also listen with oninput.

Example 3 — Clear the Field on Escape

Detect a named key on release — a common pattern for cancel / clear.

HTML
<label for="name">Name:</label>
<input type="text" id="name" onkeyup="clearOnEscape(event)">
<p id="hint">Press Escape to clear.</p>

<script>
  function clearOnEscape(e) {
    if (e.key === "Escape") {
      e.target.value = "";
      document.getElementById("hint").textContent = "Cleared.";
    }
  }
</script>
Try It Yourself

How It Works

  1. Compare e.key. Named keys use strings like "Escape" and "Enter".
  2. Act on release. Clearing after keyup avoids fighting the browser while the key is down.
  3. Use e.target. The focused input that received the event.
  4. Give feedback. A short message helps users know Escape did something.

Edge Cases & Pitfalls

Most surprises come from timing: focus changes and when the character appears in the field.

timing

Value is already updated

On keyup for a character key, the input usually already contains the new character — good for reading value.

focus

Tab can change the target

MDN notes that keydown and keyup targets can differ when focus moves (for example Tab). Do not assume they match.

modifiers

Modifier flags after release

On the keyup for Control itself, ctrlKey is false. Flags reflect state after that key is released.

IME

Composition (CJK input)

During IME composition, skip handling when event.isComposing is true so unfinished characters are not treated as final input.

paste

Paste does not fire keyup per character

For value-based filters that must catch paste, use oninput (optionally with keyup).

Browser Support

onkeyup is an HTML event handler for the keyup event in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onkeyup

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support keyup well on desktop and mobile.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
onkeyup Widely supported

Bottom line: Safe to use for keyboard release handling. Prefer addEventListener('keyup') in larger apps; inline onkeyup is fine for learning.

Key Takeaways

  • Role: run script when a key is released on a focused element.
  • vs keydown: keydown is press (can repeat); keyup is release (once).
  • vs input: use oninput when you care about value changes including paste.
  • API: use event.key / event.code, not keyCode.

One line: onkeyup handles the release — pair with onkeydown for press and oninput for value.

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the keyup event fires — when the user releases a key while the element is focused (or on document/window via script).
onkeydown fires when the key goes down (and may repeat while held). onkeyup fires once when the key is released. Use keydown for shortcuts that should feel immediate; keyup when you want the action after release.
oninput fires whenever the value changes (typing, paste, autofill). Prefer oninput for filtering field text. Use onkeyup when you specifically care that a physical key was released (for example Escape or Enter).
Yes. Unlike the deprecated keypress event, keyup fires for Escape, arrows, and most other keys.
Use event.key (and event.code for the physical key). keyCode and which are deprecated.
Yes. It is Baseline widely available (since about July 2015) across modern desktop and mobile browsers.

Next: onload

Run script when an element or the page finishes loading.

onload tutorial →

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.

8 people found this page helpful