Passing Multiple arguments to jQuery Function
You can join the selectors by commas:
$("input[name='product_id[]'], input[name='product_name[]'], input[name='manufacturer[]'], input[name='composition[]']")
.each(function() {
// ...
});
To be more DRY, use an array:
const selectorStr = ['product_id', 'product_name', 'manufacturer', 'composition']
.map(str => `input[name='${str}[]']`)
.join(',');
$(selectorStr).each(function() {
// ...
});
If you need row
to be 1
in all but the first iteration, then:
['product_id', 'product_name', 'manufacturer', 'composition']
.forEach((str, i) => {
if (i !== 0) {
row = 1;
}
$(`input[name='${str}[]']`).each(function(){
// etc
});
});
Create a common function, this will help your row logic, which gets value 1 before each iteration
function iteratorOperation(){
}
And then pass this to the iterators,
$("input[name='product_id[]']").each(iteratorOperation);
row=1;
$("input[name='product_name[]']").each(iteratorOperation);
row=1;
$("input[name='manufacturer[]']").each(iteratorOperation);
row=1;
$("input[name='composition[]']").each(iteratorOperation);
Use ,
to separate multiple selectors
$("input[name='product_id[]'],input[name='product_name[]'],input[name='manufacturer[],input[name='composition[]']").each(function() {
});
Perhaps this is interesting?
note the escaping of the []
const fieldNames = ["product_id[]", "product_name[]", "manufacturer[]", "composition[]"];
const selector = fieldNames.map(item => `input[name='${item}\\\\[\\\\]']`).join(", ")
$rows.each((row) => {
$(selector).each(() => {
let idx = fieldNames.indexOf(this.name)
row.cells[idx].innerText = $(this).val(); // jquery object or collection?
})
})