<div id="a">ONE</div>
<div id="b">TWO</div>
<div id="c">THREE</div>
var el = document.getElementsByTagName("div");
for ( var i = 0; i < el.length; i++ ) (function(){
el.addEventListener("click",function(){
console.log(this.id + " " + this.innerHTML);
},false);
})(i);
Gives me error "el.addEventListener is not a function", what is the problem?
el is a HTMLCollection, you've forgotten to use index:
el[i].addEventListener("click",function(){...};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With