Execute a JavaScript when a user writes something in an <input> field:
Write something in the text field to trigger a function.
<!DOCTYPE html> <html> <body> <input type="text" id="myInput" oninput="myFunction()"> <p id="demo"></p> <script> function myFunction() {/*from w ww. j a v a 2 s .co m*/ var x = document.getElementById("myInput").value; document.getElementById("demo").innerHTML = "You wrote: " + x; } </script> </body> </html>
The oninput event occurs when an element gets user input.
This event occurs when the value of an <input> or <textarea> element is changed.
The oninput event is similar to the onchange event.
The oninput event occurs immediately after the value of an element has changed.
The onchange occurs when the element loses focus, after the content has been changed.
The onchange event also works on <select> elements.
Bubbles: Cancelable: Event type: Supported HTML tags: | Yes No Event, InputEvent <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"> and <textarea> |
---|