Having a list of HTML anchors, I need to add Event listener, which as I understand is not recieving array when I mention it as parameter.
As a try, instead of extra functions I used self-calling, but this way even i
wasn't listened when debugging.
for (var i=0; i < anc.length; i++) {
anc[i].addEventListener('mouseover', function(i,pop,tri) {
console.log('i=%i', i); // last value of loop
pop[i].style.display = 'initial';
tri[i].style.animation='anim_dpt_tr 0.4s ease-out forwards';
});
...
}
A more useful code
var pop = document.querySelectorAll('#cities > li > a > .popup');
const anc = document.querySelectorAll('#cities > li > a');
var tri = document.getElementsByClassName('triangle-left');
for (var i=0; i < anc.length; i++) {
anc[i].addEventListener('mouseover', over(i, tri, pop));
anc[i].addEventListener('touchmove', over(i, tri, pop));
anc[i].addEventListener('touchend', out(i, tri, pop));
anc[i].addEventListener('mouseout', out(i, tri, pop));
}
function over(i,tri,pop) {
console.log("over_address=%i", pop[i]); // =0
pop[i].style.display = 'initial';
tri[i].style.animation='anim_dpt_tr 0.4s ease-out forwards';
}
function out(i,tri,pop) {
console.log("out_i=%i", i); // =each index
pop[i].style.display = 'none';
tri[i].style.animation='anim_dpt_tr_back 0.3s ease-in forwards';
}
here HTML tree
<ul id="cities">
<li>
<a href="">Shoulder
<span class="triangle-left"></span> <span class="popup">Arm</span> </a>
</li>
...
How to pass arrays correctly to Event handler?