Skip to content

Instantly share code, notes, and snippets.

@stasgavrylov
Created July 24, 2015 13:42
Show Gist options
  • Save stasgavrylov/07d63d7d32d99321ee00 to your computer and use it in GitHub Desktop.
Save stasgavrylov/07d63d7d32d99321ee00 to your computer and use it in GitHub Desktop.
Element.prototype.closest = function(selector)
{
var element = this;
do if (element.matches(selector)) return element;
while (element = element.parentElement);
return null;
};
Element.prototype.matches = (function($)
{
return $.matchesSelector
|| $.webkitMatchesSelector
|| $.mozMatchesSelector
|| $.msMatchesSelector
|| $.oMatchesSelector;
})(Element.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment