$('input[type="checkbox"]').change(function() {
var number = [];
$('input[type="checkbox"]:checked').each(function() {
number.push($(this).val());
});
if (number.length == 0) {
$('.category1').prop('checked', true);
loaddata();
} else if ($('#all').checked && number.length != 8) {
alert('it executed');
$('#all').attr('checked', false);
loaddata();
} else {
loaddata();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="expander-list" id="category">
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" id="all" class="category1">all</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="electronics" class="category1">Electronics</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="kitchen" class="category1">Kitchen</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="decoratives" class="category1">Decoratives / Interior</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="homedecor" class="category1">Home Decor</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="furnitures" class="category1">Furnitures</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="toys" class="category1">Toys</label>
</div>
</li>
<li>
<div class="radio" style="padding-left:0px">
<label>
<input type="checkbox" name="filter[]" value="vehicles" class="category1">Vehicles</label>
</div>
</li>
</ul>
My concern is very simple. by clicking on all jQuery is selecting all checkboxes but if one of them are unchecked than all checkbox are must be uncheck. but it not unchecking to all checkbox even my else if condition is not executing and it not alerting me. How to resolve it and thanks in advance.
$('#all').checked=>$('#all').is(':checked')prop()instead ofattr()for "boolean" HTML attributes. Especially avoid a mix of both.