Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active December 6, 2018 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/5b9ec6212bdde72de2e04eea7d4dcf08 to your computer and use it in GitHub Desktop.
Save andreasvirkus/5b9ec6212bdde72de2e04eea7d4dcf08 to your computer and use it in GitHub Desktop.
A simple loop that traverses the DOM tree until it finds a parent with the specified class.
export const findParentByClass = (el, className) => {
while (el.parentNode) {
el = el.parentNode
if (el.classList && el.classList.contains(className)) return el
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment