The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Programming Fundamentals" Dec 02 - 2:00 PM Eastern

Loop Over querySelectorAll Matches

Last updated on:

You can loop over Arrays really easily in JavaScript with forEach, but unfortunately it's not that simple with the results of a querySelectorAll.

/* Will Not Work */
document.querySelectorAll('.module').forEach(function() {

That's because what you get back from querySelectorAll isn't an array, it's a (non-live) NodeList.

Here's a quick way to iterate over all the found elements:

var divs = document.querySelectorAll('div');

[], function(div) {
  // do whatever = "red";

Fair warning, Todd Motto explains why this method is a rather hacky, detailing over 10 problems with it.

You could also use a classic for loop:

var divs = document.querySelectorAll('div'), i;

for (i = 0; i < divs.length; ++i) {
  divs[i].style.color = "green";

Todd's suggestion is to make your own method:

// forEach method, could be shipped as part of an Object Literal/Module
var forEach = function (array, callback, scope) {
  for (var i = 0; i < array.length; i++) {, i, array[i]); // passes back stuff we need

// Usage:
// optionally change the scope as final parameter too, like ECMA5
var myNodeList = document.querySelectorAll('li');
forEach(myNodeList, function (index, value) {
  console.log(index, value); // passes index + value back!

There are also for..of loops, but...

/* Be warned, this only works in Firefox */

var divs = document.querySelectorAll('div );

for (var div of divs) { = "blue";

This is pretty intense (probably dangerous and generally not recommended) but you could make NodeList have the same forEach function as Array does, then use it.

NodeList.prototype.forEach = Array.prototype.forEach;

var divs = document.querySelectorAll('div').forEach(function(el) { = "orange";

There is a bit more information in the MDN article.


  1. Aneece
    Permalink to comment#

    The safer way to do the last version and co-opt the Array method would be to do something like this:'div'), function(div) { console.log(div); })

    Of course, you still need to make sure you have a polyfill for Array.forEach if you’re targeting older browsers.

  2. b
    Permalink to comment#

    Hey this saved me some time – I was curious why I couldn’t loop over with forEach. Thanks!

  3. wumble
    Permalink to comment#

    there is a pretty nice function that gives you a array:

    function getDomNodeArray(selector) {
    // get the elements as a DOM collection
    var elemCollection = document.querySelectorAll(selector);

    // coerce the DOM collection into an array
    var elemArray = Array.prototype.slice.apply(elemCollection);

    return elemArray;

  4. topek
    Permalink to comment#

    ES2015 variant using spread operator:

  5. Todd
    Permalink to comment#

    If you’re using ES6, you should also be able to do this:

    Array.from(querySelectorAll('img')).forEach(img => doStuff);

    Clean and simple.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed