Your
input
event handler is using the value of the global
itemCount
variable, which will always refer to the last item.
You either need to use a local variable with the
click
event handler, or find some other way to identify the target element. For example:
let inputCounter = 1;
let rowCount = $("#myTable tbody tr").length;
const cloned = $("#myTable tbody tr:last").clone();
$(".add-row").click(function(){
const rowNumber = ++rowCount;
const newRow = cloned.clone();
$("td:eq(1)", newRow).html(rowNumber);
const items = newRow.children()[2].children[0];
items.id = "items" + rowNumber;
newRow.appendTo("#myTable tbody");
$(`input[name="inputpicker-${++inputCounter}"]`).data("itemsSelector", "#" + items.id);
});
$(document).on("input", "input[name^='inputpicker-']", function(){
const itemsSelector = $(this).data("itemsSelector");
if (itemsSelector) { $(itemsSelector).val(""); }
});