I dynamically add a table to the page using jquery, this table contains 5 dropdowns. I'm trying to populate the values of one of the dropdowns on the row that has been dynamically added, however, I'm unable to achieve it.
This is my table, I've removed the un-necessary fields to keep things simple:
<table class="table" id="1" order-month="2021-05-01T00:00:00">
<thead>
<tr>
<th scope="col">
<select class="form-select form-control mb-2 mr-sm-2" id="ItemList">
<option selected="">Select Item</option>
</select>
</th>
</tr>
<tr>
<th scope="col">Item</th>
<th scope="col">Day Part</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-select form-control mb-2 mr-sm-2" id="ClassificationList">
<option selected="">Select Classification</option>
</select>
</td>
<td>
<select class="form-select form-control mb-2 mr-sm-2" id="DayValueList">
<option selected="">Select Day Value</option>
</select>
</td>
</tr>
</tbody>
</table>
This table is added dynamically via jquery when a button is pressed on the screen, when I choose an Item from ItemList select list an ajax method is called, which in turn retrieves the classifications from the server. I then try to populate the ClassificationList select list as shown:
$.ajax({
type: 'POST',
data: formData,
url: '@Url.Page("product", "getitemclassifications")',
success: function (result) {
console.log(result);
var $dropdown = $("$('table[id=1] > tbody > tr > td > select#ClassificationList')");
$.each(result.classifications, function () {
$dropdown.append($("<option />").val(this.id).text(this.name));
});
},
error: function (result) { }
});
I've hardcoded the Id of the table in the ajax method just for testing purposes.
However, I get the following error:
Syntax error, unrecognized expression: $('table[id=1] > tbody > tr > td > select#ClassificationList')
Can anyone point me in the right direction into how you go about targeting a select list which has been dynamically added and then populating it with the given values?
$("select#ClassificationList")?