Last active
December 16, 2022 09:14
-
-
Save thinker3197/1afc76bb3c88365fc41943c6cbab5079 to your computer and use it in GitHub Desktop.
Implementation of getElementsByAttributes API for HTML DOM
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
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