HTML - attributes - oninput

revision:


Content

"oninput" attribute : when an element gets user input syntax some examples


"oninput" attribute : when an element gets user input

top

The oninput attribute fires when the value of an <<input> or <textarea> element is changed.
The "oninput attribute" is part of the event attributes, and can be used on any HTML element.

This event is similar to the "onchange event". The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus. The other difference is that the onchange event also works on <select> element.


syntax

top

<element oninput="script"></element>

value: this attribute contains a script and it works when the oninput event is triggered.


some examples

top

Write something in the text field to trigger a function.

codes:
                    <p class="spec" >Write something in the text field to trigger a function.</p>                                    
                    <input style="margin-left:3vw;" type="text" id="myInput" oninput="myInput()">
                    <p class="spec" id="demo-A"></p>
                    <script>
                        function myInput() {
                        var x = document.getElementById("myInput").value;
                        document.getElementById("demo-A").innerHTML = "You wrote: " + x;
                        }
                    </script>
                

Enter some text here:

codes:
                    <p class="spec">Enter some text here: <input type="text" id="demo-1"  oninput="addMe()"></p>
                    <p class="spec" id="sudo"></p>
                    <script>
                        function addMe() {
                            var x = document.getElementById("demo-1").value;
                            document.getElementById("sudo").innerHTML = "Entered Text: " + x;
                        }
                    </script>