Created
August 29, 2019 08:12
-
-
Save DejanBelic/9d169b2b2c00e3e572f02040d06fc2dd to your computer and use it in GitHub Desktop.
Get closest element with class name vanillajs
This file contains hidden or 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 closestByClass(el, className) { | |
while(!el.classList.contains(className)) { | |
el = el.parentNode; | |
if (!el) { | |
return null; | |
} | |
} | |
return el; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment