HTML global attributeGlobal attributes

oninput

Runs JavaScript code on every change of a field’s value: after typing a character, pasting text or moving a slider.

Applies to
<input>, <textarea>, <select> and elements with contenteditable. The event bubbles, so the attribute also works on <form>
On this page

Example

#
HTML
<label for="bio">A few words about yourself</label>
<textarea id="bio" rows="3" maxlength="80"
  oninput="document.getElementById('count').textContent = this.value.length"></textarea>
<p><span id="count">0</span> / 80 characters</p>
Preview

The counter changes with every character, including when text is pasted or deleted.

Definition and usage

#

The oninput attribute reacts to the input event, which fires every time the user changes a field’s value: types or deletes a character, pastes text, picks a suggestion, moves a slider or ticks a checkbox. It works on <input>, <textarea> and <select> elements and on elements with contenteditable.

It is the best choice when the page should react as you go: a character counter, a live preview, list filtering, a password strength meter. This is what sets it apart from onchange, which in text fields waits until the user commits the value. The input event bubbles up the tree, so one attribute on a <form> handles all the fields inside.

When the attribute sits on a field, this points to that field and this.value to its current value, always as text. Changing the value in a script, for example field.value = "", does not fire the input event. In larger projects, add the handling in a script instead of the attribute: field.addEventListener("input", …).

Syntax

#
Syntax
<input oninput="JavaScript code">

Available in the attribute’s code

#
  • this.value

    The field’s current value, always as text.
  • this.valueAsNumber

    The value converted to a number in number and range fields. Useful for calculations.
  • event.inputType

    The kind of change, for example insertText (typing a character), deleteContentBackward (Backspace) or insertFromPaste (pasting).
  • event.data

    The typed character, for example "a". When text is deleted, it is null.

More examples

#
A slider with a value preview
HTML
<label for="goal">Daily learning goal</label>
<input type="range" id="goal" min="5" max="60" step="5" value="15"
  oninput="document.getElementById('goal-value').textContent = this.value + ' min'">
<output id="goal-value" for="goal">15 min</output>
Preview

With a slider, the input event fires the whole time you drag, while change fires only after you release the handle.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Do not send a request to the server on every character. In a search with suggestions, wait a moment after the last key press (so-called debounce).
  • If only the final value matters, for example when saving settings, choose onchange. The code runs less often.

Related entries

#

Spotted a mistake or missing an example? Write to us.