HTML oninput Attribute

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

Definition and Usage

The oninput attribute is an event-handler attribute. It runs JavaScript when the input event fires — the control’s value changed from a user action (typing, pasting, checking a box, dragging a range, choosing an option, and similar).

Common on <input>, <textarea>, and <select>. Related: onchange (often when the edit is committed), onkeyup, and oninvalid.

Baseline · Widely available.

Not deprecated and not experimental. For text controls, input fires while editing; change usually waits until the value is committed (for example on blur). The event bubbles.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer el.addEventListener("input", handler) in production; use inline oninput for quick demos and learning.

HTML Syntax

syntax.html
<input|textarea|select oninput=" script">

script is JavaScript that runs on each user-driven value change. Equivalent JS: el.oninput = handler or el.addEventListener("input", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the value changesTry it
oninput="fn()"Call a named function; read this.value or an idTry it
addEventListener("input")Preferred modern listener — e.g. live character countTry it
Range / live UIUpdate a label while the user drags type="range"Try it

Live Preview

Type in the field. The status updates on every input — before you leave the field (unlike typical text change).

Control

Switch between text and range to see the same event on different controls.

Preview

Waiting for input…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Echo the Value Live

Mirror what the user types into a status line.

HTML
<label for="name">Name</label>
<input id="name" type="text" name="name"
       oninput="onType(this)">
<p id="out">You typed: </p>

<script>
  function onType(el) {
    document.getElementById("out").textContent =
      "You typed: " + el.value;
  }
</script>
Try It Yourself

How It Works

  1. Put oninput on the control — input, textarea, or select.
  2. User edits the value (type, paste, cut, etc.).
  3. input fires on each change — not only when focus leaves.
  4. Read el.value (or event.target.value) and update the UI.

Example 2 — Live Character Count

Count characters as the user types with addEventListener.

HTML
<label for="bio">Bio</label>
<textarea id="bio" name="bio" rows="3" maxlength="80"></textarea>
<p id="count" aria-live="polite">0 / 80</p>

<script>
  var bio = document.getElementById("bio");
  var count = document.getElementById("count");

  bio.addEventListener("input", function () {
    count.textContent = bio.value.length + " / 80";
  });
</script>
Try It Yourself

How It Works

  1. Listen for input on the textarea.
  2. Use value.length after each edit.
  3. aria-live announces count updates to assistive tech.
  4. maxlength caps typing; the counter still reflects the current length.

Example 3 — Range Slider Label

Show the current number while the thumb moves.

HTML
<label for="vol">Volume: <span id="vol-label">50</span></label>
<input id="vol" type="range" name="volume" min="0" max="100" value="50"
       oninput="document.getElementById('vol-label').textContent = this.value">
Try It Yourself

How It Works

  1. type="range" updates value as the user drags.
  2. oninput runs continuously during the drag — good for live labels.
  3. this.value is the current number as a string.
  4. Compare with onchange — change often fires when the user finishes the interaction.

Edge Cases & Pitfalls

Most “input never fired” bugs are setting value in script and expecting the event.

script

Programmatic value = no input

Per MDN, assigning el.value = "…" (or changing options in ways that only update via script) does not fire input. Call your update function yourself after scripted changes.

change

Not the same as change

Use input for live feedback. Use onchange when you care that the user finished editing (common for text: fire on blur after a change).

reset

Form reset

Resetting a form fires reset, not input. Re-sync labels/counters from a reset handler if needed.

checkbox

Checkbox / radio history

Modern browsers fire input when toggling checkbox/radio, but older behavior varied. Prefer also listening to change if you must support very old clients.

perf

Fires often while typing

Heavy work (network search, big DOM updates) on every keystroke can lag. Debounce expensive handlers; keep simple UI updates immediate.

Browser Support

oninput is an HTML event-handler attribute for form controls in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

oninput

Baseline widely available. Chrome, Firefox, Safari, Edge, and Opera support input on text, textarea, select, and range controls.

97% 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+
Partial / quirks
oninput Widely supported

Bottom line: Safe to use for live form UX. Prefer addEventListener('input') in apps, and remember scripted value changes do not fire input.

Key Takeaways

  • Role: value changed from a user action — live.
  • vs change: input while editing; change when committed.
  • Bubbles — good for form-level delegation.
  • Scripted value does not fire input.

One line: oninput means the user just changed the value — react immediately, or use onchange when they are done.

Frequently Asked Questions

It is an event-handler attribute. When the value of an input, textarea, or select changes from a user action (typing, pasting, moving a range, etc.), the input event fires and the JavaScript in oninput runs.
Primarily input, textarea, and select. It also applies to contenteditable hosts. Put the handler on the control whose value is changing.
For text fields, oninput fires on every edit while typing. onchange typically fires when the edit is committed — often when the control loses focus after the value changed.
No. Per MDN, assigning element.value (or similar programmatic changes) does not fire input. Only user-initiated changes (including many autofill cases) are expected to.
Yes. You can listen on a parent form with addEventListener('input', handler) for event delegation.
No. The input event and oninput handler are standard and Baseline widely available. Inline HTML handlers are discouraged in production in favor of addEventListener.

Next: oninvalid

Run JavaScript when a form control fails constraint validation.

oninvalid tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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