Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Created June 27, 2017 20:59
Show Gist options
  • Save jmlavoier/0b26bb4cf8945ef302aa27bce2566175 to your computer and use it in GitHub Desktop.
Save jmlavoier/0b26bb4cf8945ef302aa27bce2566175 to your computer and use it in GitHub Desktop.
const p = document.querySelector('.p1');
HTMLElement.prototype.parentsByTagName = function(tn) {
let arrayElements = [];
let currentEl = this;
while (currentEl) {
currentEl = currentEl.parentElement;
if (!currentEl) return arrayElements;
if (currentEl.tagName === tn) {
arrayElements.push(currentEl);
}
}
}
console.log(p.parentsByTagName('DIV')); // [div#cont.content, div, div]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment