kadarpeter
8/16/2018 - 10:08 AM

Getting DOM nodes to array

Helper function to create an array from DOM nodes. Useful for array methods.

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;
};

var divs = getDomNodeArray('div');