Created
June 27, 2017 20:59
-
-
Save jmlavoier/0b26bb4cf8945ef302aa27bce2566175 to your computer and use it in GitHub Desktop.
This file contains 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
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