I'm trying to create a form that will show more fields based on what is selected in a dropdown menu. If 1 guest is selected, then 1 form appears. If 2, then 2 forms appears and so on.
I've managed to get the first Div (guestFormOne) to show/hide depending if 1 guest has been selected, but I'm struggling to find a solution to make this happen for up to 6 guests.
Here is what I have so far:
HTML
<h4>Number of Guests</h4>
<select onchange="numofGuests()">
<option>Select number of guests</option>
<option id="guestCountOne">1</option>
<option id="guestCountTwo">2</option>
<option id="guestCountThree">3</option>
<option id="guestCountFour">4</option>
<option id="guestCountFive">5</option>
<option id="guestCountSix">6</option>
</select>
<div id="guestFormOne">
<h4>Guest 1</h4>
<input type="text" placeholder="Name">
<select id="guest1_meal">
<option>Meal Choice</option>
<option>Buffet</option>
<option>Gluten Free</option>
<option>Dairy Free</option>
<option>Vegan</option>
</select>
<select id="guest1_age">
<option>Age</option>
<option>Adult</option>
<option>Child under 5</option>
<option>Child 6 - 12</option>
</select>
<input type="text" placeholder="Allergies?">
</div>
<div id="guestFormTwo">
<h4>Guest 2</h4>
<input type="text" placeholder="Name">
<select id="guest2_meal">
<option>Meal Choice</option>
<option>Buffet</option>
<option>Gluten Free</option>
<option>Dairy Free</option>
<option>Vegan</option>
</select>
<select id="guest2_age">
<option>Age</option>
<option>Adult</option>
<option>Child under 5</option>
<option>Child 6 - 12</option>
</select>
<input type="text" placeholder="Allergies?">
</div>
Javascript
function numofGuests() {
if (document.getElementById("guestCountOne").selected) {
document.getElementById("guestFormOne").style.display = "block";
} else {
document.getElementById("guestFormOne").style.display = "none";
}
}
Help would be appreciated, as I feel like I'm stuck trying to get this to work.