Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vanilla JavaScript replacement for jQuery's .parents() method
function getParents (el, matchSelector) {
'use strict';
var parent = el.parentNode;
if ((el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(parent, matchSelector)) {
return parent;
} else {
return getParents(parent, matchSelector);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment