What\'s the native equivalent of jQuery\'s $.fn.has?
For example, how would you write the following code:
$(\"li\").has(\"ul\").css(\"background-color\",
Since i don't use jQuery, I can not make not sure if has
selects all elements which are direct parents of
elements (like CSS selector li > ul
) or selects all elements having
elements under (like CSS selector li ul
). Chose the one you like;
Array.from(document.querySelectorAll("li ul")).reduce((r, e) =>
r.length ? (r[r.length - 1] === e.parentElement ? r : r.concat(e.parentElement)) : r.concat(e.parentElement), []
);
Thanks @Barmar's comment for the clarification.