simPod
simPod

Reputation: 13486

How to determine if one of multiple object has certain class?

I have HTML lets say like:

<ul>
<li class="test"></li>
<li class="test"></li>
<li class="test"></li>
<li></li>
</ul>

Now how can I test if there's one or more li that has class test?

$("li").hasClass("test"); doesn't seem to do the job...

Thanks

Upvotes: 1

Views: 110

Answers (3)

icaru12
icaru12

Reputation: 1582

if ($("li.test").length > 0) {
    //your code
}

Upvotes: 2

mbinette
mbinette

Reputation: 5094

Now how can I test if there's one or more li that has class test?

$("li.test").size() > 0 means there is at least one li that has the class test.

Upvotes: 2

Jason Whitted
Jason Whitted

Reputation: 4024

If you have an li selected you could use .is(".test")

If you want to know how many have that class you can do $("li.test").length

Upvotes: 1

Related Questions