Skip to content

Instantly share code, notes, and snippets.

@thinker3197
Last active December 16, 2022 09:14
Show Gist options
  • Save thinker3197/1afc76bb3c88365fc41943c6cbab5079 to your computer and use it in GitHub Desktop.
Save thinker3197/1afc76bb3c88365fc41943c6cbab5079 to your computer and use it in GitHub Desktop.
Implementation of getElementsByAttributes API for HTML DOM
document.getElementsByAttribute =
Element.prototype.getElementsByAttribute = (attr, value) => {
const nodeList = document.getElementsByTagName('*'),
matchedNodes = []
for(let node of nodeList) {
if(node.hasAttribute(attr)) {
if(value === '*')
matchedNodes.push(node)
else
if(node.getAttribute(attr) === value)
matchedNodes.push(node)
}
}
return matchedNodes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment