Skip to content

Instantly share code, notes, and snippets.

@finico
Created October 20, 2015 20:14
Show Gist options
  • Save finico/591823ddb88711aef5ae to your computer and use it in GitHub Desktop.
Save finico/591823ddb88711aef5ae to your computer and use it in GitHub Desktop.
closest shim
(function (elProto) {
if (elProto.hasOwnProperty('closest')) {
return;
}
elProto.matches = elProto.matches || elProto.webkitMatchesSelector || elProto.mozMatchesSelector || elProto.msMatchesSelector || elProto.oMatchesSelector;
elProto.closest = function closest (selector) {
var element = this;
while (element) {
if (element.matches(selector)) {
break;
}
element = element.parentElement;
}
return element;
};
})(Element.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment