Javascript DOM HTML Input Text handle 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: /*from  w  w w .j  a  v  a  2 s  .  c om*/
<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