The issue is that the cloned row has the same ID as the original row, which causes issues with the dropdown. To fix this, you can update the ID of the cloned row to be unique. Additionally, the options in the dropdown are being duplicated because the option elements are being cloned along with the row. To fix this, you can remove the option elements from the cloned row and add them back in dynamically.
Here's an updated version of the JavaScript code that should fix these issues:
var template = $('#line_1').clone();
$('#cloneButton').click(function () {
var rowId = $('.row1').length + 1;
var klon = template.clone();
klon.attr('id', 'line_' + rowId)
.insertAfter($('.row1').last())
.find('option')
.remove() // remove options from cloned row
.end()
.find('textarea')
.attr('id', 'options_' + rowId) // update ID of textarea
.end()
.find('datalist')
.attr('id', 'options-list_' + rowId) // update ID of datalist
.end();
// add options back to datalist
var optionsList = klon.find('#options-list_' + rowId);
optionsList.empty();
$('#options-list option').each(function() {
optionsList.append($(this).clone());
});
});
$(document).on("click", ".remove", function() {
if($('.row1').length > 1)
$(this).closest(".row1").remove();
});
Note that this code assumes that the option elements are defined outside of the table, as in the provided code. If they are defined dynamically within the table, you may need to adjust the code accordingly.