re: How do I use .forEach on DOM Elements? VIEW POST


I think you can also do:

var timestamps = Array.from(document.getElementsByClassName("utc-time"));

Haven't tested it though 😊


Well, in the mdn documentation it had mentioned this:

However some older browsers have not yet implemented NodeList.forEach() nor Array.from()

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.

var elements = Array.from(document.querySelectorAll('p'));

// elements = (3) [p, p, p]

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 😉

code of conduct - report abuse