Last active
August 11, 2022 16:10
-
-
Save ziggi/2f15832b57398649ee9b to your computer and use it in GitHub Desktop.
Vanilla JS jQuery.parents() realisation (npm module: https://github.com/ziggi/dom-parents)
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
@LuisSevillano, check it out: https://github.com/ziggi/dom-parents