A small correction: you used document.getElementsByClassName which does not return a NodeList but a HTMLCollection. Now, the former does have forEach defined - but it's pretty much the only array method that has been added to its prototype so far.
But it's only a relatively recent addition, so older browsers don't support it - fortunately, the Array#forEach trick works pretty well, down to sufficiently old Internet Explorer versions (probably 6? 5.5? The heck am I saying, that could work for slice, but forEach was added only in IE9...).
A HTMLCollection is a totally different beast... and something that should be avoided in general. It's a live collection that gets updated when the DOM changes. Quite heavy when it comes to memory consumption and CPU usage.
Conclusion: use document.querySelectorAll instead (which returns a NodeList).
... a simple for statement would have worked. And would probably be a bit safer.
I don't agree with that Β―\_(γ)_/Β―
It's true that every browser supports for (duh!), but experience proved that something that iterates over a collection for us is simpler as it doesn't force us to take care of a variable for counting, while the (relatively) complex - although well-known - syntax of for is prone to mistakes.
HTMLCollections are array-like. They are iterable and have a length property. The slice function can convert array-like objects into Arrays. this is bound to our array-like object. slice iterates over this using the length property since no other arguments were given. All the elements are returned in a new Array then we can call forEach method on our Array
Is a good solution too if not better. timestamps is bound to this then forEach method iterates over the array-like object by using the length property. My solution would have looped through the array-like object once then the newly created array versus this solution which is once.
Eventually, I realized that timestamps was not an array, it was a NodeList and at the top of mdn documentation, ...
I seldom use selectElementsByClassName, but according to mdn it returns a HTMLCollection. And the mdn doc clearly says it hasn't got forEach.
But for NodeList, you should be able to do forEach. If you do document.querySelectorAll('.className'), you will get an NodeList and you should be able to do forEach. See here.
Howeverrrrr, since most older browsers won't have NodeList.prototype.forEach defined, it is probably safer to do what you suggested Array.prototype.forEach.call(elements, ...) or just [].forEach.call(elements, ...). A more "es6" way would probably be Array.from(elements).forEach(...).
Orrrrr, you could do it with for-loops as you suggested. "es6" introduced this amazing for-of loop, it could loop through most list-like things. So the following would work as well.
for(constelofelements){// ...}
Of course, to safely use ES6 features you probably want your polyfills + babel set up to support old browsers.
Although it's a very good point that you can use a for-loop in this case, your own solution can be simplified to [].forEach.call, which would also work in case you'd need other array operations such as map, filter or reduce.
Hi @jess
and anyone who may enter for the foreach on dom elements, or NodeList Iteration class.
I recommend a gist that I found using a lot in my current web Proyect. Iterate over a NodeList
This isn't related to the purpose of your post, but why are you creating a new variable (localTime), and is there a reason you're using a span instead of the time element?
I've always used [].forEach.call(nodeList, n => n.something), however be careful if in that loop you are removing or adding nodes. Found that out the hard way.
Ah this is a classic rites of passage in DOM unintutiveness.
If only
NodeListinherited fromArray...There are some methods that don't make much sense in relation to
NodeLists. Mostly mutating methods likepush,sortorsplice.The others are fine, though. I'd love to have
maporfilterπuse the "β¦" spread operator, which works for both
getElementsByClassName()andquerySelectorAll()so you can port easily later.I think you can also do:
Haven't tested it though π
Well, in the mdn documentation it had mentioned this:
So I assumed that if forEach wasn't supported, Array.from wouldn't be either?
I just tested it and it works on Chrome for me.
It might not be compatible with many browsers though, as you mentioned.
You don't have to go far - if you have Windows and IE11 installed, that doesn't support either π
The MDN documentation also has an Array.from polyfill:
developer.mozilla.org/en-US/docs/W...
A small correction: you used
document.getElementsByClassNamewhich does not return aNodeListbut aHTMLCollection. Now, the former does haveforEachdefined - but it's pretty much the only array method that has been added to its prototype so far.But it's only a relatively recent addition, so older browsers don't support it - fortunately, the
Array#forEachtrick works pretty well, down to sufficiently old Internet Explorer versions (probably6? 5.5?The heck am I saying, that could work forslice, butforEachwas added only in IE9...).A
HTMLCollectionis a totally different beast... and something that should be avoided in general. It's a live collection that gets updated when the DOM changes. Quite heavy when it comes to memory consumption and CPU usage.Conclusion: use
document.querySelectorAllinstead (which returns aNodeList).I don't agree with that Β―\_(γ)_/Β―
It's true that every browser supports
for(duh!), but experience proved that something that iterates over a collection for us is simpler as it doesn't force us to take care of a variable for counting, while the (relatively) complex - although well-known - syntax offoris prone to mistakes.Mostly caused by distraction and/or boredom π
I just want to make a few points.
forloop in years.querySelectorAll.getElementsByClassNamereturns anHTMLCollection, which is why you were getting the error.NodeLists have aforEachmethod andHTMLCollections do not.My initial instinct on circumventing this is the following:
HTMLCollections are array-like. They areiterableand have alengthproperty. Theslicefunction can convert array-like objects intoArrays.thisis bound to our array-like object.sliceiterates overthisusing thelengthproperty since no other arguments were given. All the elements are returned in a newArraythen we can callforEachmethod on ourArrayBut looking at your initial answer:
Is a good solution too if not better. timestamps is bound to
thisthenforEachmethod iterates over the array-like object by using thelengthproperty. My solution would have looped through the array-like object once then the newly created array versus this solution which is once.Wait, is this true?
I seldom use
selectElementsByClassName, but according to mdn it returns aHTMLCollection. And the mdn doc clearly says it hasn't gotforEach.But for
NodeList, you should be able to doforEach. If you dodocument.querySelectorAll('.className'), you will get anNodeListand you should be able to doforEach. See here.Howeverrrrr, since most older browsers won't have
NodeList.prototype.forEachdefined, it is probably safer to do what you suggestedArray.prototype.forEach.call(elements, ...)or just[].forEach.call(elements, ...). A more "es6" way would probably beArray.from(elements).forEach(...).Orrrrr, you could do it with for-loops as you suggested. "es6" introduced this amazing
for-ofloop, it could loop through most list-like things. So the following would work as well.Of course, to safely use ES6 features you probably want your polyfills + babel set up to support old browsers.
A best practice is to convert a
NodeListto a normalArray, so you can use the built-in forEach/map functions.One ES5 way is to create a helper function, for example on the NodeList prototype:
Another popular way is to create a jQuery-ish helper function that wraps the conversion to array. This is also used in Lea Verou's bliss.js library:
The modern ES7+ way is to use the
...operator:Although it's a very good point that you can use a for-loop in this case, your own solution can be simplified to [].forEach.call, which would also work in case you'd need other array operations such as map, filter or reduce.
Hi @jess and anyone who may enter for the foreach on dom elements, or NodeList Iteration class.
I recommend a gist that I found using a lot in my current web Proyect. Iterate over a NodeList
This isn't related to the purpose of your post, but why are you creating a new variable (localTime), and is there a reason you're using a
spaninstead of thetimeelement?I've always used
[].forEach.call(nodeList, n => n.something), however be careful if in that loop you are removing or adding nodes. Found that out the hard way.developer.mozilla.org/en-US/docs/W...
Isn't
NodeList.prototype.forEachexists?Guess her browser hasn't implemented it
And I've learned too, thanks =)
Awesome!
I always seem to forget that .forEach() only works on Arrays and not objects. Thank you
this works, thank you!.
woohoo!