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 withcontenteditable. The event bubbles, so the attribute also works on<form>
Example
#<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>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
#<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 innumberandrangefields. Useful for calculations.event.inputType
The kind of change, for exampleinsertText(typing a character),deleteContentBackward(Backspace) orinsertFromPaste(pasting).event.data
The typed character, for example"a". When text is deleted, it isnull.
More examples
#<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>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
#- onchangeRuns JavaScript code when the user commits a new field value: picks an option, ticks a checkbox or finishes typing.
- <input>A form field: text, email, password, number, date, slider, checkbox, file and many more.
- <textarea>A multi-line text field for longer input: messages, reviews, descriptions.
- <output>The result of a calculation or a user action, for example a total in a calculator.
- onkeydownRuns JavaScript code the moment a key is pressed. Used for keyboard shortcuts and for handling keys such as Enter or Escape.
Spotted a mistake or missing an example? Write to us.