Skip to content

Instantly share code, notes, and snippets.

@monochromer
Created April 20, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monochromer/4b5d8c200b45af4b15b9a55fb1c7daf4 to your computer and use it in GitHub Desktop.
Save monochromer/4b5d8c200b45af4b15b9a55fb1c7daf4 to your computer and use it in GitHub Desktop.
closest polyfill
;(function(Element) {
// matches polyfill
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.matchesSelector ||
Element.prototype.webkitMatchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
function matches(selector) {
var element = this,
elements = (element.document || element.ownerDocument).querySelectorAll(selector),
index = 0;
while (elements[index] && elements[index] !== element) {
++index;
}
return elements[index] ? true : false;
};
};
//closest polyfill
if (!Element.prototype.closest) {
Element.prototype.closest = function(selector) {
var node = this;
while (node) {
if (node.matches(selector)) return node;
else node = node.parentElement;
}
return null;
};
}
})(Element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment