Skip to content

Instantly share code, notes, and snippets.

@gpDA
Created February 23, 2022 18:39
Show Gist options
  • Save gpDA/a56974cf5c70128af7a9283b31c095f3 to your computer and use it in GitHub Desktop.
Save gpDA/a56974cf5c70128af7a9283b31c095f3 to your computer and use it in GitHub Desktop.
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