Last active
June 10, 2020 11:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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