I have 2 listeners: one invokes another. Question is if second (nested) listener knows where in the document the first listener was triggered? I mean when I use addEventListener for 50 fields in a column and within this listener invoke another listener then the second one 'knows' the position (I mean field where it was clicked) of the first one? I try to cope with this problem - don't know if somehow second listener 'inherits' variables' values from the first one. E.g:
document.querySelectorAll("#decisionList").forEach(elem => elem.addEventListener("change", function () {
var selectedOptionIndex = this.options[this.selectedIndex].index;
var invoiceDateText = this.closest('tr').querySelector('.payment').textContent.trim();
var finalChoice = this.closest('tr').querySelector('.choice');
switch (selectedOptionIndex) {
case 0:
finalChoice.innerHTML = '<input type="date">'
break;
case 1:
finalChoice.innerHTML = '<input id="finalDate" type="date">'
finalDateListener(selectedOptionIndex, invoiceDateText); //here I'm passing variables keeping current values from main Listener
default:
finalChoice.innerHTML = ''
}}));
function finalDateListener(selectedOptionIndex, invoiceDateText){
const finalDate = document.getElementById("finalDate"); //'id' from parent Listener
finalDate.addEventListener("change", function () {
alert(invoiceDateText + ' ' + selectedOptionIndex);
});
}
This code works only for first triggering of second listener (so when I invoke first listener and after that the second one), for the next ones it doesn't work, no alerts shows up. Does that mean that I need to again look for closest element in second listener?