I try to add two event listeners to the same DOM elements like this:
const rows = document.querySelectorAll(`.row`);
for (let i = 0; i < rows.length; i++) {
rows[i].addEventListener(`touchend`, (e) => {
console.log(i)
if (documentClick) {
const code = rows[i].dataset.code;
}
});
rows[i].addEventListener(`click`, (e) => {
console.log(i)
const code = rows[i].dataset.code;
});
}
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
<div class="row">x</div>
When I check the index of clicked element in console it gives me different index. How can it be and why does it happen?