Say I have an array of DOM elements based on any selector
var elems = document.querySelectorAll(selector);
I have no idea what's included in elems
, but let's assume that elems.length > 0
I'd like to use querySelectorAll (or some equivalent function) on elems
to find all elements that match an additional selector.
// example: find all buttons with class `foo` within `elems` array
var buttons = elems.querySelectorAll("button.foo");
This doesn't work (for obvious reasons), but I'm unsure of how to do it otherwise :(
Here's the wrapper I created to work with the accepted answer from @Tibos
// Element.matches wrapper
(function(e){
if (typeof e.matches !== "function") {
e.matches = e.webkitMatchesSelector ||
e.mozMatchesSelector ||
e.msMatchesSelector ||
e.oMatchesSelector ||
e.matchesSelector;
}
})(Element.prototype);