Reputation: 55
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
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
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