Reputation: 1802
I've crated a check box group and failed to implement a requirement.
If all checkboxes are unselected within the group, the Top-Level checkbox should be displayed in a unselected state.
I am doing it in a same way as i did it for all selected checkbox. My Third else if statement is not working.
I am trying to fill the three requirement:
If not all checkboxes are selected within the filter grouping, the Top-Level Filter displays ( - ) in the checkbox.
If all checkboxes are selected within the filter grouping, the Top-Level Filter displays selected state with checkmark.
If all checkboxes are unselected within the group, the Top-Level checkbox should be displayed in a unselected state.
I am failed to achieve the last statement.
JS:
<script>
$(".faceted-filters").on("click", "#checkAll", function(){
$(this).parents(".top-lvl-filter").next(".sub-lvl-filter").find("input[type=checkbox]").prop('checked', $(this).prop('checked'));
$(this).removeClass("partially-checked");
});
$("#collapse-one input").change(function(){
var a = $("#collapse-one input");
if(a.length !== a.filter(":checked").length){
$(this).parents(".sub-lvl-filter").prev().find("input").addClass("partially-checked");
}
else if(a.length == a.filter(":checked").length){
$(this).parents(".sub-lvl-filter").prev().find("input").attr('checked','checked');
$(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
}
else if(a.length == a.filter.not(":checked").length){
$(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
}
});
</script>
Working example: http://codepen.io/anon/pen/PqjJdO
Upvotes: 4
Views: 1490
Reputation: 111
This below change will solve your problem,
$("#collapse-one input").change(function(){
var a = $("#collapse-one input");
if(a.length > a.filter(":checked").length && a.filter(":checked").length > 0){
$(this).parents(".sub-lvl-filter").prev().find("input").addClass("partially-checked");
}
else if(a.length == a.filter(":checked").length){
$(this).parents(".sub-lvl-filter").prev().find("input").prop('checked','checked');
$(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
}
else if(a.filter(":checked").length < 1){
$(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
$(this).closest(".composite-filter").find(".top-lvl-filter input").prop('checked','');
}
});
Upvotes: 1
Reputation: 572
Your first condition is hiding the 3rd condition to happen.
Try this -
$("#collapse-one input").change(function(){
var a = $("#collapse-one input");
if(a.filter(":checked").length==0){
$(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
$(this).parents(".sub-lvl-filter").prev().find("input").removeAttr('checked','checked');
}
else if(a.length !== a.filter(":checked").length){
//alert('all checked');
$(this).parents(".sub-lvl-filter").prev().find("input").addClass("partially-checked");
}
else if(a.length == a.filter(":checked").length){
$(this).parents(".sub-lvl-filter").prev().find("input").attr('checked','checked');
$(this).closest(".composite-filter").find(".top-lvl-filter input").removeClass("partially-checked");
}});
Upvotes: 0