-
-
Save m-arrieta-r/9237f66137bf1258ae788597bcbc0483 to your computer and use it in GitHub Desktop.
Vanilla JS jQuery.parents() realisation
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
Element.prototype.parents = function(selector) { | |
var elements = []; | |
var elem = this; | |
var ishaveselector = selector !== undefined; | |
while ((elem = elem.parentElement) !== null) { | |
if (elem.nodeType !== Node.ELEMENT_NODE) { | |
continue; | |
} | |
if (!ishaveselector || elem.matches(selector)) { | |
elements.push(elem); | |
} | |
} | |
return elements; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment