Reputation: 117
I have a table with a number of 'selects' in each table row.
I need to iterate through each table row and append 'options' to the selects from an array. Currently only the first 'select' in the table is populated with data from the array.
I have the following HTML:
<table id="myTable">
<thead>
<tr>
<th>Code</th>
<th>Date</th>
<th>Description</th>
<th>Percentage</th>
</tr>
</thead>
<tbody>
<tr class="RepeatingSection">
<td>
<select id="InputCode"></select>
</td>
<td>
<input type="date" name="Date" id="DateInput" required="required" />
</td>
<td>
<input type="text" id="DetailsInput" name="Details" size="35" required="required" />
</td>
<td>
<input type="number" id="percentageInput" max="100" value="100" />%</td>
</tr>
<tr class="RepeatingSection">
<td>
<select id="InputCode"></select>
</td>
<td>
<input type="date" name="Date" id="DateInput" required="required" />
</td>
<td>
<input type="text" id="DetailsInput" name="Details" size="35" required="required" />
</td>
<td>
<input type="number" id="percentageInput" max="100" value="100" />%</td>
</tr>
<tr class="RepeatingSection">
<td>
<select id="InputCode"></select>
</td>
<td>
<input type="date" name="Date" id="DateInput" required="required" />
</td>
<td>
<input type="text" id="DetailsInput" name="Details" size="35" required="required" />
</td>
<td>
<input type="number" id="percentageInput" max="100" value="100" />%</td>
</tr>
</tbody>
I currently have the following JavaScript:
var array;
var i;
var option;
var select;
array = [001, 002, 003, 004];
for (i = 0; i < array.length; i++) {
option = document.createElement("option");
option.value = array[i];
option.text = array[i];
select = document.getElementById("InputCode");
$('#myTable .RepeatingSection').each(function() {
$(this).find('#InputCode').each(function() {
select.appendChild(option);
})
})
select.appendChild(option);
}
Please also see my fiddle
Any help would be appreciated. Hope this post makes sense.
Upvotes: 0
Views: 1117
Reputation:
Try following JavaScript code:
var array;
var i;
var option;
array = [001, 002, 003, 004];
for (i = 0; i < array.length; i++) {
option = document.createElement("option");
option.value = array[i];
option.text = array[i];
$('#myTable .RepeatingSection').find( "select[id=InputCode]" ).append(option);
}
Upvotes: 1