Skip to content

Instantly share code, notes, and snippets.

@KaKi87
Last active February 20, 2020 07:43
Show Gist options
  • Save KaKi87/27f182e731ac30282619754aa7df2fbc to your computer and use it in GitHub Desktop.
Save KaKi87/27f182e731ac30282619754aa7df2fbc to your computer and use it in GitHub Desktop.
Get all parents of HTML element
const getParents = element => {
const parents = [];
for (; element && element !== document; element = element.parentElement){
parents.push(element);
}
return parents;
};
/*
Source : https://gomakethings.com/how-to-get-all-parent-elements-with-vanilla-javascript/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment