Detecting click elements of an array

Asked

Viewed 27 times

0

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

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

1 answer

1


You can iterate all the span and add the event click:

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);
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.