Skip to content

Instantly share code, notes, and snippets.

@kikill95
Created February 23, 2016 15:01
Show Gist options
  • Select an option

  • Save kikill95/159e115839b9009487b5 to your computer and use it in GitHub Desktop.

Select an option

Save kikill95/159e115839b9009487b5 to your computer and use it in GitHub Desktop.
forEach for NodeList
// 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++) {
callback.call(scope, 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!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment