Skip to content

Instantly share code, notes, and snippets.

@DejanBelic
Created August 29, 2019 08:12
Show Gist options
  • Save DejanBelic/9d169b2b2c00e3e572f02040d06fc2dd to your computer and use it in GitHub Desktop.
Save DejanBelic/9d169b2b2c00e3e572f02040d06fc2dd to your computer and use it in GitHub Desktop.
Get closest element with class name vanillajs
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