👀 Live Preview
Type below — the character count updates on every input:
0 / 100 characters

The oninput attribute is an inline event handler that runs JavaScript whenever the input event fires — when a form control’s value changes in real time. Unlike onchange, which often waits until editing is finished, oninput responds on every keystroke, paste, cut, or slider move. Use it for live character counts, instant search filters, range slider labels, and soft validation feedback. It works on <input>, <textarea>, and <select>. Prefer addEventListener("input", …) in production; inline oninput is great for learning.
Inline JS.
Real-time.
input, textarea.
When to use.
Preferred way.
Live updates.
oninput AttributeThe primary purpose of oninput is to give users immediate feedback as they interact with a form. When someone types in a search box, you can filter results on each character. When they drag a range slider, you can update a label instantly. When they paste text, the handler runs right away — no need to wait for blur.
Because input can fire very frequently, keep handlers lightweight or debounce expensive work (like API calls). For final validation before submit, many apps still use onchange or onblur alongside oninput for softer live hints.
oninput = “value changed now.” onchange = “user committed a new value” (for text fields, usually after leaving the field). Use both when you need live preview and final validation.
Set oninput on a form control, or assign element.oninput in script:
<input type="text" oninput="myFunction()">
<input type="range" id="volume" oninput="updateValue(this.value)">input event fires.input, textarea, and select.value without user action (in modern browsers).element.oninput = function() { … }.element.addEventListener("input", handler).input event bubbles — delegation on a parent form works.The oninput attribute accepts a string of JavaScript code:
oninput="myFunction()" — Call a named function.oninput="updateValue(this.value)" — Pass the current value.field.oninput = () => { … } assigns the handler.const search = document.getElementById("search");
search.addEventListener("input", () => {
const query = search.value.trim().toLowerCase();
filterResults(query);
});| Action | input fires? | Notes |
|---|---|---|
| Type a character | Yes | Every keystroke |
| Paste text | Yes | Immediate update |
| Drag range slider | Yes | Classic oninput use |
| Tab away (no value change) | No | blur fires instead |
JS sets value only | No | Not a user action |
| Handler attribute | oninput | Inline on element |
| Target | Supported? | Notes |
|---|---|---|
<input type="text"> | Yes | Search, names, live count |
<input type="range"> | Yes | Slider value display |
<textarea> | Yes | Multi-line input |
<select> | Yes | Option changes |
<input type="checkbox"> | Yes | Toggle state (also fires change) |
<button> | No | No editable value |
oninput vs onchange vs onkeyup| Handler | When it fires | Typical use |
|---|---|---|
oninput | Every value change (real time) | Live search, character count |
onchange | Committed change (blur for text) | Final validation, submit prep |
onkeyup | Key released on keyboard | Keyboard-only; misses paste/drag |
Range slider live value, addEventListener, and character count as you type.
Type below — the character count updates on every input:
0 / 100 characters
Update a label as the user drags the slider:
<input type="range" id="rangeInput" min="0" max="100" value="50"
oninput="updateValue(this.value)">
<p id="output">Current Value: 50</p>
<script>
function updateValue(value) {
document.getElementById("output").textContent =
"Current Value: " + value;
}
</script>Range inputs fire input continuously while dragging — perfect for live feedback without waiting for change.
Attach the handler with addEventListener or the property form:
<input type="text" id="dynamicInput">
<script>
document.getElementById("dynamicInput").addEventListener("input", function () {
console.log("Input detected!");
});
// Or property form:
document.getElementById("dynamicInput").oninput = function () { /* … */ };
</script>Register once at page load. Typing, paste, and cut all trigger the same handler.
Show remaining characters as the user types in a textarea:
const bio = document.getElementById("bio");
const counter = document.getElementById("counter");
const max = 200;
bio.addEventListener("input", () => {
const left = max - bio.value.length;
counter.textContent = left + " characters remaining";
});Read element.value.length inside the handler. Update a counter or warning style when the user nears the limit.
aria-live="polite" on counters or validation messages updated by oninput.input harm usability.<label for> so screen readers know what field is updating.Type, paste, or drag.
Control reflects change.
oninput handler runs.
Search, count, slider label.
The input event and oninput handler are supported in all modern browsers on form controls — Chrome, Firefox, Safari, and Edge.
oninput supportAll major browsers fire the input event and honor the oninput handler on editable form elements.
Bottom line: Use oninput confidently for real-time form feedback; prefer addEventListener in production.
oninput for live search, counts, and slider labelsaddEventListener("input", …) in production codeonchange when you need final validation tooaria-live for accessible live feedbackonkeyup when you need paste and drag support — use oninputalert() on each input eventvalue changes fire inputoninput to non-editable elements like buttonThe oninput attribute runs JavaScript whenever a form control’s value changes in real time — ideal for responsive search, character limits, and range sliders.
Prefer addEventListener("input", …), keep handlers fast, and combine with onchange when you need validation after the user finishes editing.
oninputBookmark these before building live form interactions.
Every change.
Eventinput, textarea.
ScopeLive vs commit.
CompareDrag updates.
PatternForm delegate.
DOMinput event fires — whenever the user changes a form control’s value in real time.input, textarea, and select — elements with an editable or selectable value.oninput fires on every value change while typing. onchange usually fires when the user finishes editing (e.g. leaves the field).form for all child inputs.addEventListener("input", handler) is preferred for production. Inline oninput works for learning.Practice the oninput attribute with slider and character-count examples in the Try It editor.
5 people found this page helpful