Created
November 7, 2023 08:24
-
-
Save bjsi/afd7e73c302383d59c87e28b907a7e1a to your computer and use it in GitHub Desktop.
Get HTML element ancestor stack
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 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