Javascript DOM onchange Event handle input text field change event

Introduction

Execute a JavaScript when a user changes the content of an input field:

Modify the text in the input field, then click outside the field to fire the onchange event.

View in separate window

<!DOCTYPE html>
<html>
<body>
Enter some text: //ww  w.j  av a 2s .c o  m
<input type="text" 
       name="txt" 
       value="Hello" 
       onchange="myFunction(this.value)">

<p id="demo"></p>
<script>
function myFunction(val) {
  document.getElementById("demo").innerHTML = "The input value has changed. The new value is: " + val;
}
</script>

</body>
</html>



PreviousNext

Related