Skip to content

Instantly share code, notes, and snippets.

@tpgmartin
Created May 2, 2017 00:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpgmartin/fdeb556bc4c7da69453bcbfc3e71ae52 to your computer and use it in GitHub Desktop.
Save tpgmartin/fdeb556bc4c7da69453bcbfc3e71ae52 to your computer and use it in GitHub Desktop.
Implementing 'getElementsByClassName' from scratch in JavaScript
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