Javascript Form How to - Link two select elements








Question

We would like to know how to link two select elements.

Answer


<!--from w ww  .  j  a  va2 s.c  o m-->
<!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'>

document.getElementById("box1").onchange = function(){
    document.getElementById("box2").value = this.value;
};

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

The code above is rendered as follows: