Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Element.closest(selector) polyfill
/**
* Element.closest(selector) polyfill
*
* https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
*/
(function(){
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}
if (!Element.prototype.closest) {
Element.prototype.closest = function(s) {
var el = this;
do {
if (el.matches(s)) return el;
el = el.parentElement || el.parentNode;
} while (el !== null && el.nodeType === 1);
return null;
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.