I am using the following code to show/hide divs based on a dropdown selection. However, I want each higher number to keep the previous divs displayed and simply add another div. The user is selecting the "number of passengers" so the option selected should display that many divs.
<script>
function display_passengerDiv(e){
document.getElementById('passenger1').style.display = "none";
document.getElementById('passenger2').style.display = "none";
document.getElementById('passenger3').style.display = "none";
document.getElementById('passenger' + e).style.display = "block";
}
</script>
<select name="#" id="#" onChange="display_passengerDiv(this.selectedIndex);">
<option selected="selected"> </option>
<option >1</option>
<option >2</option>
<option >3</option>
</select>
<div id="passenger1" style="display:none;"> hey, 1 works </div>
<div id="passenger2" style="display:none;"> hey, 2 works </div>
<div id="passenger3" style="display:none;"> hey, 3 works </div>
How can I do this?
First we get the value of the select and then use it to go through the divs. The elms is the divs with name passenger starting with elms[0] or passenger1. This code first hides all divs then reshows the total number that is selected.
I assume you want all three divs to display when 3 is selected? If so :
CSS CODE
JS CODE
See this full script here
Try this: