JQuery: Add a class if the child of a particular class does not exist

I want to make addclass, but only if it does not have a child of a particular class.

$selector.addClass('addthisclass');

So I want:

<div class="addthisclass"><span></span></div>

And nothing here.

<div><span class="badclass"></span></div>

Does anyone know how to do this?

+3
source share
1 answer

Use :hasand :notselectors:

$("div:not(:has(span.badclass))").addClass("addthisclass");
+10
source

Source: https://habr.com/ru/post/1713288/


All Articles