Say I have an array of DOM elements based on any selector
var elems = document.querySelectorAll(selector);
I do not know what is included in elems, but suppose thatelems.length > 0
I would like to use querySelectorAll (or some equivalent function) on elemsto find all the elements that match the optional selector.
// example: find all buttons with class `foo` within `elems` array
var buttons = elems.querySelectorAll("button.foo");
This does not work (for obvious reasons), but I'm not sure how to do it otherwise :(
Here's a wrapper that I created to work with the accepted answer from @Tibos
(function(e){
if (typeof e.matches !== "function") {
e.matches = e.webkitMatchesSelector ||
e.mozMatchesSelector ||
e.msMatchesSelector ||
e.oMatchesSelector ||
e.matchesSelector;
}
})(Element.prototype);
source
share