oninput event

Definition and usage

The oninput event occurs when the element receives user input.

This event occurs when the value of an <input> or <textarea> element changes.

Tip:This event is similar to onchange händelse. The difference is that the oninput event occurs immediately after the element value changes, while onchange occurs when the element loses focus and the content changes. Another difference is that the onchange event also applies to <select> elements.

Instance

Example 1

Execute JavaScript when the user writes content in the <input> field:

<input type="text" oninput="myFunction()">

Try it yourself

Example 2

Range slider - how to dynamically update the slider value:

<input type="range" oninput="myFunction(this.value)">

Try it yourself

Syntax

In HTML:

<element oninput="myScript">

Try it yourself

In JavaScript:

object.oninput = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("input", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Stöd
Cancelable: Not supported
Event type: Event, InputEvent
Supported HTML tags: <input type="color">, <input type="date">, <input type="datetime">, <input type="email">, <input type="month">, <input type="number">, <input type="password">, <input type="range">, <input type="search">, <input type="tel">, <input type="text">, <input type="time">, <input type="url">, <input type="week"> och <textarea>
DOM version: Nivå 3-händelser

Webbläsarstöd

Numrerna i tabellen anger den första webbläsaren som helt stöder händelsen.

Händelser Chrome IE Firefox Safari Opera
oninput Stöd 9.0 4.0 5.0 Stöd