I\'m trying to set get id of all elements in an HTMLCollectionOf
. I wrote the following code:
var list = document.getElementsByClassName(\"event
There's no reason to use es6 features to escape for
looping if you're on IE9 or above.
In ES5, there are two good options. First, you can "borrow" Array
's forEach
as evan mentions.
But even better...
Object.keys()
, which does have forEach
and filters to "own properties" automatically.That is, Object.keys is essentially equivalent to doing a for... in
with a HasOwnProperty
, but is much smoother.
var eventNodes = document.getElementsByClassName("events");
Object.keys(eventNodes).forEach(function (key) {
console.log(eventNodes[key].id);
});