TSCAmerica.com
TSCAmerica.com

Reputation: 5377

Jquery Exclude a checkbox from select all

My HTML code to select all checkboxes

Select All <input type="checkbox"  name='select_all' id='select_all'  value='1'/>

My Javascript code to select all checkboxes

<script type="text/javascript">
    $('#select_all').change(function() {
    var checkboxes = $(this).closest('form').find(':checkbox');
    if($(this).is(':checked')) 
    {
       checkboxes.prop('checked', true);
                } else {
        checkboxes.prop('checked', false);
        }
    });
    </script>

The code works great and selects all the checkboxes, I would like to exclude the following checkbox from the selection (select all) criteria, is it possible to exclude the following?

 <input type="checkbox"  name='sendtoparent' id='sendtoparent'  value='1'/>

Upvotes: 1

Views: 1523

Answers (2)

Pabs123
Pabs123

Reputation: 3435

Try using the not method:

$(this).closest('form').find(':checkbox').not('#sendtoparent');

Upvotes: 3

Rory McCrossan
Rory McCrossan

Reputation: 337580

Firstly you can use :not or not() to exclude the element by its id attribute. From there you can simplify the logic by just setting the checked property of those checkboxes to match that of the #select_all element. Try this:

$('#select_all').change(function() {
  var $checkboxes = $(this).closest('form').find(':checkbox').not('#sendtoparent');
  $checkboxes.prop('checked', this.checked);
});

Upvotes: 1

Related Questions