Created
February 23, 2022 18:39
-
-
Save gpDA/a56974cf5c70128af7a9283b31c095f3 to your computer and use it in GitHub Desktop.
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
function getElementsByClassName(className) { | |
const results = []; | |
const checkRecursive = (element, cName) => { | |
const cn = element.getAttribute("class"); | |
if (cn !== undefined && cn !== null && cn.includes(cName)) { | |
results.push(element); | |
} | |
// element.children MDN | |
if (element.children.length > 0) { | |
for (const e of element.children) { | |
checkRecursive(e, cName); | |
} | |
} | |
}; | |
checkRecursive(document.body, className); | |
return results; | |
} | |
console.log(getElementsByClassName("pikachu").length); // 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment