Detect clicking elements in an array

0

If I have a div with id="banner-tooltips" , and within it I have n <span> tags declared in variables with javascript, how do I detect individual click in these span tags?

var caixaTooltips = document.getElementById("banner-tooltips");
var tooltips = caixaTooltips.querySelectorAll("span");
    
asked by anonymous 22.03.2017 / 12:35

1 answer

1

You can iterate all span and add the click event:

var caixaTooltips = document.getElementById("banner-tooltips");
var tooltips = caixaTooltips.querySelectorAll("span");

for (let i = 0 ; i < tooltips.length ; i++){
    tooltips[i].addEventListener('click', function(e){
        console.log("Fui clicado: " + e);
    });
}
    
22.03.2017 / 12:39