Like your selector with jQuery you can use querySelectorAll and go search all elements that are descendants of <li>
and use a for
loop to iterate them and tie an event handler to each one.
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 click handler (to detect the click event) on <li>
directly. If you later want to know which descendant element of <li>
that was clicked you 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>
})