Last active
February 11, 2024 20:33
-
-
Save dutchcelt/a0d127d53d92c04a7c85e6f0553f5f05 to your computer and use it in GitHub Desktop.
A function to get elements by Attribute name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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