JavaScript: matchesSelector

Published on Author craigzearfossLeave a comment
/*
Returns true if the specified selector matches.

Usage:
    matchesSelector(document.getElementById('myDiv'), 'div.someSelector[some-attribute=true]')

reference: http://davidwalsh.name/essential-javascript-functions
*/

function matchesSelector(el, selector) {
   var p = Element.prototype;
   var f = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector || function(s) {
       return [].indexOf.call(document.querySelectorAll(s), this) !== -1;
   };
   return f.call(el, selector);
}

Leave a Reply

Your email address will not be published. Required fields are marked *