oninput event


oninput Event

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p>在文本框中尝试输入触发函数。</p>
<input type="text" id="myInput" oninput="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myInput").value;
    document.getElementById("demo").innerHTML = "你输入的是: " + x;
}
</script>

</body>
</html>

Run Instance»

Click "Run instance" button to view the online instance



Definition and usage

The oninput event is triggered when the user inputs.

The event occurs in <input> or <textarea> Fires when the value of the element changes.

Tips: This event is similar to the onchange event. The difference is that the oninput event is triggered immediately when the element value changes. onchange is triggered when the element loses focus. Another difference is that the onchange event can also act on <keygen> and <select> element.


Browser support

The number in the table indicates the version number of the first browser that supports the event.

##oninputYes9.04.05.0Yes
event





##Syntax

HTML:

< ;
element
oninput="myScript">Try it
JavaScript:

object
.oninput=function(){myScript}; Try it
In JavaScript, use the addEventListener() method:

object
.addEventListener("input", myScript); Try it
Note:

Internet Explorer 8 and earlier IE versions do not support addEventListener( ) method.

Technical details

Whether bubbling is supported: Is it possible to cancel: Event type: Supported HTML tags:
Yes
No
Event
<input type="password">, <input type="search">, <input type="text"> and <textarea>