I have built a form and there is a part where there is an "add row" button which adds another row of inputs (if the user needs it).
However on this row there is a select
input that needs a change
handler. So what I have done, is every time the button to add a new row is clicked, each of the select
inputs (which have the same name
attribute) are given a unique id
like so
let creditorInputs = $('select[name="creditors"]');
console.log(creditorInputs);
creditorInputs.map(i => {
$(creditorInputs[i]).attr('id', `creditorSelect${i}`);
This gives them an id of "creditorSelect0, creditorSelect1" etc
But further down the loop I have added a change
listener and when I click on the select input an change its value, it only seems to affect the first one on the page.
Here is the full script:
$('#addrowothermort').click(() => {
let creditorInputs = $('select[name="creditors"]');
console.log(creditorInputs);
creditorInputs.map(i => {
$(creditorInputs[i]).attr('id', `creditorSelect${i}`);
$(`#creditorSelect${i}`).change(() => {
console.log("Changed");
});
});
});
EDIT:
$('#tabs-6').click(event => {
event.preventDefault();
if(event.target.name == 'creditors') {
let rightInput = event.target;
rightInput.addEventListener('change', () => {
console.log(rightInput.value);
})
}
})