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(" />
    <input type="button" id="btn" name="btn" value="Submit" />
    <hr />
    TextBox value:
    <label id="lblText"></label>
       function CallFunction(id) {
         document.getElementById("lblText").innerHTML = document.getElementById(id).value;

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.


 Views: 1091 | Post Order: 34

Write for us

Hosting Recommendations