Evanss
Evanss

Reputation: 23613

If no divs in a selection have a class then do something?

With jQuery how can I run a function if no divs in a selection have a certain class?

There are multiple div.result. Initially they will all have a class of "hidden" but in time none of them will. Im running the following on different events:

  if (!$(".result").hasClass("hidden")) {
    $("body").addClass("all-shown");
  }

Upvotes: 0

Views: 25

Answers (1)

Turnip
Turnip

Reputation: 36702

Check the length of the returned set...

if (!$(".result.hidden").length) {
    $("body").addClass("all-shown");
}

Example...

if (!$(".result.hidden").length) {
    alert("no items");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Upvotes: 1

Related Questions