Skip to content

Instantly share code, notes, and snippets.

@Demwunz
Last active June 10, 2020 11:11
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 Demwunz/557f2d0bbcaceff26e055269df5fae8f to your computer and use it in GitHub Desktop.
Save Demwunz/557f2d0bbcaceff26e055269df5fae8f to your computer and use it in GitHub Desktop.
Little function that returns the matching node by classname. Useful in all browsers
function closestEl(elem, elClass) {
var node = elem
while (node.parentElement) {
node = node.parentElement
if (node.classList.contains(elClass)) {
break
}
}
node = node.classList.contains(elClass) ? node : elem
return node
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment