Skip to content

Instantly share code, notes, and snippets.

@sumitpore
Created May 3, 2020 07:28
Show Gist options
  • Save sumitpore/2c740e6e34db4d2231361a965618acb5 to your computer and use it in GitHub Desktop.
Save sumitpore/2c740e6e34db4d2231361a965618acb5 to your computer and use it in GitHub Desktop.
jQuery closest's in Pure JavaScript
if (window.Element && !Element.prototype.closest) {
Element.prototype.closest =
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i,
el = this;
do {
i = matches.length;
while (--i >= 0 && matches.item(i) !== el) {};
} while ((i < 0) && (el = el.parentElement));
return el;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment