Skip to content

Instantly share code, notes, and snippets.

@m-arrieta-r
Forked from ziggi/parents.js
Created August 11, 2017 19:32
Show Gist options
  • Save m-arrieta-r/9237f66137bf1258ae788597bcbc0483 to your computer and use it in GitHub Desktop.
Save m-arrieta-r/9237f66137bf1258ae788597bcbc0483 to your computer and use it in GitHub Desktop.
Vanilla JS jQuery.parents() realisation
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