JavaScript > Events

Onchange in JavaScript

How to executes a function when element value changes in JavaScript?


In case we want to execute a function when the element value is changing, we can follow this approach.

 Enter your name:
    <input type="text" name="txtName" id="txtName" onchange="CallFunction(this.id)" />
    <input type="button" id="btn" name="btn" value="Submit" />
    <hr />
    TextBox value:
    <label id="lblText"></label>
    
    <script>
       function CallFunction(id) {
         document.getElementById("lblText").innerHTML = document.getElementById(id).value;
       }
    </script>

In the above code snippet, we have a textbox on which onchange event has been specified that executes “CallFunction()” function. In this function we are setting the Label (lblText) inner html value to the texbox(txtName) value.

OUTPUT

 Views: 1013 | Post Order: 34



Write for us