👀 Live Preview
Type in the search box — results filter each time you release a key:
- Apple
- Banana
- Orange
- Grape

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.
Inline JS.
Key released.
Which key.
Up vs down.
Preferred way.
Filter lists.
onkeyup AttributeThe 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 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.
Set onkeyup on a focusable element, or assign element.onkeyup:
<input type="text" onkeyup="handleKeyUp(event)">
<script>
function handleKeyUp(e) {
console.log("Key released:", e.key);
}
</script>keyup event fires.document.addEventListener("keyup", …).keydown for the same physical press.event to read key, code, and modifier flags.oninput for that.element.addEventListener("keyup", handler).keyup event bubbles.The onkeyup attribute accepts a string of JavaScript code:
onkeyup="handleKeyUp()" — Call a named function.onkeyup="searchFunction()" — Run search after key release.field.onkeyup = (e) => { … } assigns the handler.searchInput.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
runSearch(searchInput.value);
return;
}
filterResults(searchInput.value);
});| Action | keyup fires? | Notes |
|---|---|---|
| Release letter key in input | Yes | After keydown |
| Release Enter | Yes | Common submit trigger |
| Release Escape | Yes | Close/clear patterns |
| Paste text (no key up in field) | No | Use oninput |
| Key still held down | No | Waits for release |
| Handler attribute | onkeyup | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<input>, <textarea> | Yes | When focused |
<button>, <select> | Yes | Keyboard activation |
tabindex="0" on div | Yes | Custom keyboard targets |
document / window | Yes | Via addEventListener |
<div> without tabindex | No | Not focusable |
onkeyup vs onkeydown vs oninput| Handler | When it fires | Typical use |
|---|---|---|
onkeydown | Key pressed down | Shortcuts, prevent default early |
onkeyup | Key released | After keystroke, Enter handling |
oninput | Value changed | Live search, character count |
Live filter on key release, addEventListener, and Enter to run search.
Type in the search box — results filter each time you release a key:
Filter results when the user releases a key in the search field:
<label for="search">Search:</label>
<input type="text" id="search" onkeyup="searchFunction()">
<p id="searchResult"></p>
<script>
const fruits = ["Apple", "Banana", "Orange"];
function searchFunction() {
const query = document.getElementById("search").value.toLowerCase();
const matches = fruits.filter(f => f.toLowerCase().includes(query));
document.getElementById("searchResult").textContent =
matches.length ? "Results: " + matches.join(", ") : "No results";
}
</script>Read input.value inside the handler — the character from the key you just released is already in the field.
Attach the handler with addEventListener:
<input type="text" id="dynamicInput">
<script>
document.getElementById("dynamicInput").addEventListener("keyup", function (e) {
document.getElementById("log").textContent = "Key released: " + e.key;
});
</script>Register once at page load. Every key release while the field is focused triggers the callback.
Run search when the user releases Enter:
searchInput.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
runSearch(searchInput.value.trim());
}
});Filter on every keyup, but only call heavy runSearch when Enter is released — a basic debounce-style split.
preventDefault() on Tab in keyup handlers.aria-live="polite" on search result regions.<button type="submit"> for keyboard users.keydown fires first.
Physical key up.
onkeyup handler runs.
Search, Enter, feedback.
The keyup event and onkeyup handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge.
onkeyup supportAll major browsers fire keyup and honor the onkeyup handler on focusable elements.
Bottom line: Reliable for keyboard release handling; pair with oninput when value changes matter more than key timing.
event.key to detect Enter, Escape, or specific keysaddEventListener("keyup", …) in productiononinput when paste and autofill must trigger updatesonkeydown when you need both press and releaseoninputkeyCode in new codeonkeyup to non-focusable elements without tabindexThe 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.
onkeyupBookmark these before adding keyboard release handlers.
After keydown.
EventEnter, Escape.
APIValue changes.
CompareRead .value.
PatternAPI calls.
Perfkeyup event fires — when the user releases a key while the element is focused.onkeydown = key pressed down. onkeyup = key released. keydown always fires first.oninput for filtering by text — it handles paste too. onkeyup still works for simple demos.oninput for paste.addEventListener("keyup", handler) is preferred for production. Inline onkeyup works for learning.Practice the onkeyup attribute with live filter and Enter examples in the Try It editor.
5 people found this page helpful