Javascript Form How to - Handle onchange event on select input








Question

We would like to know how to handle onchange event on select input.

Answer


<!DOCTYPE html>
<html>
<body>
  <select id="box1">
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
  <select id="box2">
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
<script type='text/javascript'>
<!--from   w  w w  .  ja va 2  s  .  c om-->
document.getElementById("box1").onchange = function(){
    document.getElementById("box2").value = this.value;
};

</script>
</body>
</html>

The code above is rendered as follows: