Skip to content

Instantly share code, notes, and snippets.

@dutchcelt
Last active February 11, 2024 20:33
Show Gist options
  • Save dutchcelt/a0d127d53d92c04a7c85e6f0553f5f05 to your computer and use it in GitHub Desktop.
Save dutchcelt/a0d127d53d92c04a7c85e6f0553f5f05 to your computer and use it in GitHub Desktop.
A function to get elements by Attribute name
const getElementsByAttribute = (target, attr) => {
const nodeArray = [];
const considerNode = node => node.hasAttribute(attr) ? 1 : 3;
const treeWalker = document.createTreeWalker(target, 1, { acceptNode: considerNode }, false);
while(treeWalker.nextNode()) nodeArray.push(treeWalker.currentNode);
return nodeArray
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment