Get an array of the Element‘s ancestors, starting from the parent, up to the <html> element, optionally filtered by a callback function.
.getAncestors(callback = undefined)true, if an item should be added (optional ).<html> element, unless it is called on the <html> element itself, in which case this method returns an empty array.A simple example could look like this:
document.getElementById('foo') .getAncestors() .forEach( (item) => { … });
By using a filtering function, e.g. to only collect ancestors which are <li> elements:
let list = document.getElementById('foo') .getAncestors( e => e.nodeName == 'LI' );
.nodeName property always contains the element name in uppercase letters!