The :has() CSS pseudo-class represents an element if any of the selectors passed as parameters (relative to the :scope of the given element) match at least one element.
/* Selects any <a>, as long as it has an <img> element directly inside it */ let test = document.querySelector('a:has(> img)');