Hacker News new | ask | show | jobs
by no_gravity 3107 days ago
Unfortunately, querySelectorAll returns a NodeList, not an Array. So you cannot use forEach() on it.
1 comments

It's a very recent addition. IE11 doesn't have it for sure.
Not in Edge though.
The page I linked has a compatibility chart that says Edge 16 has it (no idea if that's a planned release, haven't ever used Edge).
You can use Array.prototype.forEach.call(nodelist, fn), or Arrays.from(nodelist).forEach
I don't use Windows so I can't test it, but would this work as a polyfill?

    NodeList.prototype.forEach = Array.prototype.forEach;
In Chrome this even returns true:

    NodeList.prototype.forEach === Array.prototype.forEach
Tested in IE11, document.documentMode===11

  Array.prototype.forEach.call(document.querySelectorAll('div'), console.log)
seams to work