2

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');
    }
});
Community
  • 1
  • 1
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • possible duplicate of [Jquery "select all" checkbox](http://stackoverflow.com/questions/15879608/jquery-select-all-checkbox) – Dom May 26 '14 at 05:56
  • Check out my answer, it does what you want and deselects the "check all" box when you select a different box. – Ian May 26 '14 at 06:03

6 Answers6

3

add a class on child checkboxes and do like this in one line:

HTML:

<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>

JQUERY:

$('#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);
}

})

FIDDLE DEMO

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

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);

});
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
1

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');
    });

DEMO

Ian
  • 653
  • 3
  • 12
0

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');
    }
});
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

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

Ram Singh
  • 6,664
  • 35
  • 100
  • 166
0

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); 
     } 
 });
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57