Javascript - How to make auto select another drop down

wow picture wow · Dec 29, 2009 · Viewed 7.8k times · Source
  • Here have two list field menu. First brand second item
  • I want if we select brand IBM that item will select IBM too
  • In other hand, if we select brand HP that item will select HP too

How to do that in javascript.

<select name="brand">
  <option>Please Select</option>
  <option>IBM</option>
  <option>HP</option>
</select>

<select name="item">
  <option>Please Select</option>
  <option>IBM</option>
  <option>HP</option>  
</select>

Answer

Sampson picture Sampson · Dec 29, 2009

I noticed your options line up with one another, so you could simply reflect the selectedIndex in in the second from the first:

document.getElementById("brand").onchange = function(){
  document.getElementById("item").selectedIndex = this.selectedIndex;
}