Skip to content

Instantly share code, notes, and snippets.

@atesztoth
Created May 26, 2019 22:27
Show Gist options
  • Save atesztoth/9475225363bc018cd12eb7d8d72b8998 to your computer and use it in GitHub Desktop.
Save atesztoth/9475225363bc018cd12eb7d8d72b8998 to your computer and use it in GitHub Desktop.
This little scripts looks for children of an element that has a class name
function getChildWithClassName(element, className) {
if (!element || !className) return null;
const toIterable = elemList => (elemList ? [...elemList] : [])
const searcher = nodes => {
if (!nodes || nodes.length < 1) return null;
return nodes.find(node => toIterable(node.classList).includes(className))
|| nodes.map(node => searcher(toIterable(node.childNodes))).find(x => x) // 🚀
}
return searcher(toIterable(element.childNodes));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment