Skip to content

Instantly share code, notes, and snippets.

@shanelonergan
Last active February 4, 2020 23:00
Show Gist options
  • Save shanelonergan/20f2541c04680574ff11a1eb93c0a73e to your computer and use it in GitHub Desktop.
Save shanelonergan/20f2541c04680574ff11a1eb93c0a73e to your computer and use it in GitHub Desktop.
function getElementsByClassName2(classNameStr) {
function checkChildren(child) {
// check if the child has a matching class. If so, push the the elements array
if (child.classList.contains(classNameStr)) {
elements.push(child)
}
// check if that child has children of its own. If so, call checkChildren one each child
if (child.children) {
const children = child.children
children.forEach(checkChildren)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment