Skip to content

Instantly share code, notes, and snippets.

@bjsi
Created November 7, 2023 08:24
Show Gist options
  • Save bjsi/afd7e73c302383d59c87e28b907a7e1a to your computer and use it in GitHub Desktop.
Save bjsi/afd7e73c302383d59c87e28b907a7e1a to your computer and use it in GitHub Desktop.
Get HTML element ancestor stack
function getAncestorStack(element) {
let ancestorStack = [];
while (element && element !== document.body) {
let elementDetails = {
tagName: element.tagName,
classes: Array.from(element.classList),
dataAttributes: Object.assign({}, element.dataset)
};
ancestorStack.push(elementDetails);
element = element.parentNode;
}
console.log(ancestorStack)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment