I'm creating a form. In the input section, I'm using a table to organize the input lines. When the pages loads, currently one line is being shown. If the user needs additional lines, then the user clicks the button labeled additional and it clones the div. I've been able to change the id of each additional div that gets created, but I need to also change the id of the underlying elements. This is the html portion:
<table id="input_values">
<tr id="addtl_country_0">
<td>
<select name="countryToVisit" id="countryToVisit" required="">
<option selected disabled value="select-country">Select Country</option>
<option value="uk">United Kingdom</option>
<option value="germany">Germany</option>
<option value="chinahk">China/Hong Kong</option>
</select>
</td>
</tr>
</table>
So far for the JavaScript portion, I have:
let countriesCounter = 1;
function addtlCountry() {
let addtlRow = document.getElementById('addtl_country_0');
let table = document.getElementById('input_values');
let clone = addtlRow.cloneNode(true);
clone.id = "addtl_country_" + countriesCounter;
table.appendChild(clone);
countriesCounter++;
}
With the javascript I currently have, each additional div id changes: addtl_country_0 => addtl_country_1. I would like to be able to do something similar with the select ID ie, "countryToVisit" => "countryToVisit_1".
I'm also open to hearing if there are any better ways of doing this. The "output" of this input is to create a static table that displays the chosen country in the first column, subsequent columns will depend on that country.