Is there a function similar to jQuery .closest() but for traversing descendants and returning only closest ones?
I know that there is
In case someone's looking for a pure JS solution (using ES6 instead of jQuery), here's the one I use:
Element.prototype.QuerySelector_BreadthFirst = function(selector) {
let currentLayerElements = [...this.childNodes];
while (currentLayerElements.length) {
let firstMatchInLayer = currentLayerElements.find(a=>a.matches && a.matches(selector));
if (firstMatchInLayer) return firstMatchInLayer;
currentLayerElements = currentLayerElements.reduce((acc, item)=>acc.concat([...item.childNodes]), []);
}
return null;
};