JavaScript onchange Event Example

Updated on     Kisan Patel

If we want to execute a javascript function when the element value is changing, we need to use javascript onchange event

<!-- HTML -->
Name: <input type="text" name="txtName" id="txtName" onchange="TestFunction(this.id)" />
<br/>
<label id="lblName"></label>

<!-- Javascript -->
<script type="text/javascript">
function TestFunction(id) {
   document.getElementById("lblName").innerHTML = document.getElementById(id).value;;
}
</script>

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

See the Pen zGXGPB by Kisan (@pka246) on CodePen.


JavaScript

Leave a Reply