Similar to your selector with jQuery you can use the querySelectorAll and fetch all the elements that are descended from <li>
and use a for
loop for loops and tie an Event Handler to each.
Example (live here):
var descendentes = document.querySelectorAll("#list a");
for (var i = 0; i < descendentes.length; i++) {
descendentes[i].addEventListener("click", function (e) {
alert('O elemento clicado foi o ' + this.innerHTML);
})
}
Another option is to add a Handler click (to detect the click event) on the <li>
directly. If you then want to know which descending element of <li>
that was clicked can use event.target
.
Example (live here):
var li = document.getElementById("list");
li.addEventListener("click", function(event) {
console.log(event.target); // este é o elemento clicado
alert('O elemento clicado foi o ' + e.target.innerHTML);
// dentro desta função o "this" refere-se ao <li>
})
Robson, Welcome to Stackoverflow! Can you complete your answer with example code for this question? Just as the answer is unclear, in addition the link you joined goes to an English page which does not help those who do not speak English.
– Sergio