I need to get all DOM elements within DOM element myRootElement, that satisfy the following conditions:
- have one of several element names (e.g.
divorblockquote) - have one of several class names (e.g.
fooorbar)
If it were just condition 1, I know I can write:
let elementsWithRightName = myRootElement.querySelectorAll("div, blockquote");
and then I can filter the result, like so:
let isOfNiceClass = (elem) => ["foo", "bar"].includes(elem.className);
let elementsOfInterest = [...elementsWithRightName].filter(isOfNiceClass);
but is there a better/most elegant/idiomatic way to apply both filters?
Use
.matches():Or, with the new-ish
:is()/:where():For more complex cases, it depends. However, I'd say a CSS selector is probably the most efficient way to search for elements: browsers optimize it under the hood so we don't have to. I'm not sure about XPath though.