Created
May 2, 2017 00:50
-
-
Save tpgmartin/fdeb556bc4c7da69453bcbfc3e71ae52 to your computer and use it in GitHub Desktop.
Implementing 'getElementsByClassName' from scratch in JavaScript
This file contains 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 getElementsByClassName2(className) { | |
const nodes = [] | |
function crawl(node) { | |
if (node.classList && node.classList.value.indexOf(className) > -1) { | |
nodes.push(node) | |
} | |
node.childNodes.forEach((child) => | |
crawl(child) | |
) | |
} | |
crawl(document.body) | |
return nodes | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment