4 ms·
document.getElementsByClassName('clicky').forEach(function(el) { el.addEventListener('click', doSomething); });
by MatthewPhillips 14y ago
document.getElementsByClassName('clicky').forEach(function(el) { el.addEventListener('click', doSomething); });
- d4n3 14y agoI'm not sure that works. getElementsByClassName (and Id and querySelector) returns a NodeList, not an Array, so there's no foreach, you have to convert it to an array first.
- MatthewPhillips 14y agoArray.prototype.slice.call(document.getElementsBy....
- mark_story 14y agoThat's also a ton of tying and a lot of code that a minifier can't reduce. I guess its possible, but do you really want to writing such verbose code?