Trying to create a form for a travel website. For one of the features I want to have the user select how many children they want to take on their trip. Subsequently, I then want the form to add new select fields the number of which depends on the amount of children selected. This is what I have so far:
HTML:
<select id="children">
<option value ="0">0</option>
<option value ="1">1</option>
<option value ="2">2</option>
<option value ="3">3</option>
<option value ="4">4</option>
<option value ="5">5</option>
<option value ="6">6</option>
<option value ="7">7</option>
</select>
<div id="childrenAge"></div>
JS
$("select#children").on("change", function() {
var number = parseInt($("#children").val());
var newDropLabel = $("#childrenAge").append("<label>Age of children: </label>");
var newDropList = $('<select class="Age"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option></select>');
$("div#childrenAge").append(newDropLabel);
for (i = 0; i < number; i++) {
$("div#childrenAge").append(newDropList);
};
});
After selecting a number, this produces the new label and only one new select element. I've checked and the number variable gives the correct number value output. Not quite sure why it's not working.
I also want to add a label to each new select field, something like "Child 1" which increments each time depending on the amount of children chosen.
Any tips would be amazing.
Thanks.
2is selected, you want to add two age-selection<select>elements, one for each child?