I mean an array of them. That is a chain from top HTML to destination element including the element itself.
for example for element <A>
it would be:
[HTML, BODY, DIV, DIV, P, SPAN, A]
To get the parent node of an HTML element, you can use the parentNode property. This property returns the parent node of the specified element as a Node object. The parentNode property is read-only, which means you can not modify it.
parentNode gives the parent, while . parentElement gives undefined.
A little shorter (and safer, since target
may not be found):
var a = document.getElementById("target"); var els = []; while (a) { els.unshift(a); a = a.parentNode; }
You can try something like:
var nodes = []; var element = document.getElementById('yourelement'); nodes.push(element); while(element.parentNode) { nodes.unshift(element.parentNode); element = element.parentNode; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With