drizo
drizo

Reputation: 55

Check if one of li doesnt have a class

I'm having trouble with this condition : I have a series of li with a class, and I want to execute code if one of the li does not have this class.

For example :

<ul class="list-synthese">
 <li class="hidden">Lorem</li>
 <li class="hidden">Lorem</li>
 <li class="hidden">Lorem</li>
 <li class="hidden">Lorem</li>
 <li>Lorem</li>
</ul>

Here is what I want to do :

if $('list.synthese > li').hasNOTclass('hidden'){
 mycode;
}

Hope someone can help me ! Thanks ! :)

Upvotes: 2

Views: 178

Answers (2)

Naeem Shaikh
Naeem Shaikh

Reputation: 15715

A bad approach but working: Fiddle

$(function() {
  $('.list-synthese > li').each(function(i, ele) {

    if (!$(ele).hasClass('hidden')) {
      alert('li number ' + (i + 1) + '  doesnt have class hidden')
    }

  })

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list-synthese">
  <li class="hidden">Lorem</li>
  <li class="hidden">Lorem</li>
  <li class="">Lorem</li>
  <li class="hidden">Lorem</li>
  <li>Lorem</li>
</ul>

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use :not selector. also you have incorrect selector for ul element:

if($('.list-synthese > li:not(.hidden)').length){
  mycode;
}

Upvotes: 4

Related Questions