Would like a vanilla JS solution, ie no jquery etc.
I have several
Some of these contain a with the class Bar (not as a direct child).
I want to add the class Baz to any Foo
Any assistance would be wonderful. Many thanks!
This is what I have so far. I am sure the contains syntax is where I am getting this wrong.
const addClassTo = document.querySelectorAll('li.foo').contains('div.bar');
addClassTo.forEach((element) => {element.classList.add('baz');})
;
>Solution :
const addClassTo = Array.from(document.querySelectorAll('li.foo')).filter((ele) => ele.querySelector('div.bar'))
addClassTo.forEach((element) => {element.classList.add('baz');})