Skip to content

Instantly share code, notes, and snippets.

@shelldandy
Created March 19, 2018 04:55
Show Gist options
  • Save shelldandy/da437ea6a134dca26a4ceec42b88e893 to your computer and use it in GitHub Desktop.
Save shelldandy/da437ea6a134dca26a4ceec42b88e893 to your computer and use it in GitHub Desktop.
Make a document.querySelectorAll loopable
/**
* Make a document.querySelectorAll loopable
* @author Todd Motto
* @tutorial https://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack
* @param {NodeList} elements
* @param {Function} callback function that takes element and index
* @param {this} scope what this refers to
*/
const loopQuery = (elements, callback, scope) => {
for (let i = 0; i < elements.length; i++) {
callback.call(scope, elements[i], i)
}
}
export default loopQuery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment