Reputation: 17382
I want to check all checkboxes when 'Select all' checkbox is checked. I've read various threads on SO, but not able to get the expected o/p.
Related Question:- Select all checkboxes with jQuery
Here's what I tried.
<div class="row-fluid" id="set_alarm">
<label class="checkbox inline"><input type="checkbox" id="select_all"><b>Select All</b></label><br>
<label class="checkbox inline days"><input type="checkbox" id="Mon"><b>Mon</b></label>
<label class="checkbox inline days"><input type="checkbox" id="Tue"><b>Tue</b></label>
......
......
</div>
jQuery
$('#select_all').change(function() {
var checkboxes = $(this).closest('.days').find(':checkbox');
if($(this).is(':checked')) {
checkboxes.attr('checked', 'checked');
} else {
checkboxes.removeAttr('checked');
}
});
Upvotes: 2
Views: 4620
Reputation: 28513
Try below to select all checkboxes when select_all
checked
$('#select_all').change(function() {
$('.days').prop("checked", $(this).is(':checked'));
});
and unselect select_all
when one of the days
checkbox unchecked.
$('.days').change(function() {
if($(this).is(':checked'))
{
// check if all days checkbox checked then check select_all
if($('.days').is(':checked').length == $('.days').length)
$('#select_all').prop("checked", true);
}
else
{
$('#select_all').prop("checked", false);
}
});
Upvotes: 0
Reputation: 20636
You can also use this, if you don't want to modify your markup. DEMO Fiddle
$('#select_all').change(function() {
$('.days input[type="checkbox"]').attr("checked", this.checked);
});
$('.days input[type="checkbox"]').prop("checked", this.checked);
OR
$('.days input[type="checkbox"]').attr("checked", this.checked);
Following is the code to unselect Select All if you uncheck on of the days.
$('.days input[type="checkbox"]').change(function() {
$('#select_all').attr("checked", this.checked);
});
Upvotes: 1
Reputation: 62488
add a class on child checkboxes and do like this in one line:
<div class="row-fluid" id="set_alarm">
<label class="checkbox inline"><input type="checkbox" id="select_all"><b>Select All</b></label><br>
<label class="checkbox inline days"><input type="checkbox" class="days" id="Mon"><b>Mon</b></label>
<label class="checkbox inline days"><input type="checkbox" class="days" id="Tue"><b>Tue</b></label>
</div>
$('#select_all').change(function() {
$('.days').prop("checked", this.checked);
});
$('.days').change(function(){
if($('input:checkbox:checked.days').length === $("input:checkbox.days").length)
{
$('#select_all').prop("checked",true);
}
else
{
$('#select_all').prop("checked",false);
}
})
Upvotes: 3
Reputation: 653
You need to generalize your code more, there is a bunch of stuff in that other issue that isn't needed if you're just trying to select all checkboxes.
$('#select_all').change(function() {
if($(this).is(':checked')) {
$("input[type='checkbox']").attr('checked', 'checked');
} else {
$("input[type='checkbox']").removeAttr('checked');
}
});
$("input[type='checkbox']").not('#select_all').change( function() {
$('#select_all').removeAttr('checked');
});
Upvotes: 1
Reputation: 6918
Here is the simple example
$('#select_all').click(function() {
var c = this.checked;
$(':checkbox').prop('checked',c);
});
OR
$('#select_all').click(function() {
$(':checkbox').prop('checked', this.checked);
});
That's all
Upvotes: 0
Reputation: 85545
closest is for parent tree not next or previous siblings you can use nextAll:
var checkboxes = $(this).parent().nextAll('.days').find(':checkbox');
$('#select_all').change(function() {
var checkboxes = $(this).parent().nextAll('.days').find(':checkbox');
if($(this).is(':checked')) {
checkboxes.prop('checked', true);
} else {
checkboxes.removeProp('checked');
}
});
Upvotes: 0