Skip to content

Instantly share code, notes, and snippets.

@ottonascarella
Created June 26, 2016 06:34
Show Gist options
  • Save ottonascarella/3ea0b0954d012cb0a492fb06459f0786 to your computer and use it in GitHub Desktop.
Save ottonascarella/3ea0b0954d012cb0a492fb06459f0786 to your computer and use it in GitHub Desktop.
Polyfill matches DOM method (depends on querySelectorAll)
/* improved version of the one found at https://developer.mozilla.org/docs/Web/API/Element/matches */
(function(p) {
if (p.matches) return;
p.matches =
p.matchesSelector ||
p.mozMatchesSelector ||
p.msMatchesSelector ||
p.oMatchesSelector ||
p.webkitMatchesSelector ||
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) { /* */ }
return i > -1;
};
}(Element.prototype));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment