I have couple of drop downs whose values are readily available as below.
Dropdown 1:
<select id="a">
<option value="1">Bike</option>
<option value="2">Car</option>
<option value="3">Bus</option>
<option value="4">Cycle</option>
<option value="5">Wagon</option>
</select>
Dropdown 2:
<select id="b">
<option value="1">2 wheel</option>
<option value="2">4 wheel</option>
<option value="3">6 wheel</option>
</select>
I need to change second dropdown value based on the selected value from first dropdown. For example:
- If Bike or Cycle is selected from dropdown 1, 2 wheel should be auto selected
- If Car or Wagon is selected, 4 wheel should be auto selected.
Only option available for me is to use JavaScript. I tried like below, but it isn't working. Please help pointing out what am I missing/doing incorrect.
HTML:
<select id="a" onchange="change();">
<option value="1">Bike</option>
<option value="2">Car</option>
<option value="3">Bus</option>
<option value="4">Cycle</option>
<option value="5">Wagon</option>
</select>
<select id="b">
<option value="1">2 wheel</option>
<option value="2">4 wheel</option>
<option value="3">6 wheel</option>
</select>
JS:
function change() {
if (document.getElementById('a').value == '1')
document.getElementById("b").value = '1';
else if (document.getElementById('a').value == '2')
document.getElementById("b").value = '2';
else if (document.getElementById('a').value == '3')
document.getElementById("b").value = '3';
else if (document.getElementById('a').value == '4')
document.getElementById("b").value = '1';
else if (document.getElementById('a').value == '5')
document.getElementById("b").value = '2';
};
EDIT: To be more specfic about what I need, I edited the example above.
After your explanation, I would go with this:
Actually your code works too - perhaps your browser does not like the function name
change