const productUrl = $("#product-container").data("url"); function createProductSelectOptions(callback) { $.ajax({ url: productUrl, method: "GET", success: function (data) { let options = ''; data.forEach((product) => { options += ``; }); callback(options); }, error: function () { alert("Gagal memuat produk."); }, }); } $(document).ready(function () { // Initial load only for the first row createProductSelectOptions((options) => { $(".product-select").first().html(options); }); // When adding a new row $(document).on("click", ".btn-add-row", function () { const row = `
`; const $newRow = $(row); $("#product-container").append($newRow); // Load options only for the new select createProductSelectOptions((options) => { $newRow.find(".product-select").html(options); }); }); // Remove row $(document).on("click", ".btn-remove-row", function () { $(this).closest(".product-row").remove(); }); });