13

I am trying to select all checkboxes on a page once the 'select all' checkbox is clicked and i am using jquery for this as you can see at the below link:

http://jsfiddle.net/priyam/K9P8A/

The code to select and unselect all checkbox is:

function selectAll() {
    $('.selectedId').attr('checked', isChecked('selectall'));
}

function isChecked(checkboxId) {
    var id = '#' + checkboxId;
    return $(id).is(":checked");
}

After being stuck with it for a day, I am still not sure why I cant get it to work. Please help

11 Answers 11

24

Why don't you do it this way? It is more clear and readable

$('#selectall').click(function () {
    $('.selectedId').prop('checked', this.checked);
});

$('.selectedId').change(function () {
    var check = ($('.selectedId').filter(":checked").length == $('.selectedId').length);
    $('#selectall').prop("checked", check);
});

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

Good solution, using it (!). Little simplification: var check = ( $('.selectedId').length == $('.selectedId:checked').length );
thanks. lately I have been using :checked pseudo selector as well
Thanks. your code is very simple and is better because when click on Select All and then uncheck one of checkboxes, then selectall checkbox change to uncheck. LIKE!
Props for the change function. It makes the interaction more polished.
12

Your fiddle works after I made 3 fixes :

  • import of jQuery (top left menu)
  • addition of the missing isChecked function
  • use of prop instead of attr to check the boxes

10 Comments

@destroy thanks for help...but even after the fix if you click on selectall thrice it stops working
this only works for the first time, try checking/un-checking again
@wizkid Maybe you're trying the initial version, but the last edit (more than 6 minutes ago though) fixed it.
@dystroy why? was there a specific reason.
@dystroy; thats strange. Do you have allergy with reputations. why don't you transfer some to me..
|
7

It is because the method selectAll is not avaible in the global scope.

In the second dropdown in the left panel Frameworks and Extensions, select no -wrap head/body it will work fine.

The reason being by default onload is selected and when onload is selected all the entered script is wrapped inside a anonymous function as given below. It will make the selectAll function a local method inside the anonymous function, thus your onclick event handler which uses global scope will not get access to the method causing an Uncaught ReferenceError: selectAll is not defined error.

$(window).load(function(){
    //Entered script
});

Demo: Fiddle

Update But you can simplify it as

$(function(){
    $('#selectall').click(function(i, v){
        $('.selectedId').prop('checked', this.checked);
    });

    var checkCount = $('.selectedId').length;
    $('.selectedId').click(function(i, v){
        $('#selectall').prop('checked',$('.selectedId:checked').length  == checkCount)
    });
});

and remove all the onclick handlers from input elements as shown in this demo

Comments

4

Use this...

$('#selectall').on('click', function() {
    $('.selectedId').attr('checked', $(this).is(":checked"));
});

An see this DEMO

2 Comments

ops, simple but not "unselect" the selectall-checkbox. See @letiagoalves solution.
@PeterKrauss yes, i forgot the code to unselect the checkbox #selectall. The code of letiagolves it works.
4
$(document).ready(function () {
    $('#selectall').click(function () {
        $('.selectedId').prop('checked', $(this).is(":checked"));
    });
});

more Optimized

1 Comment

same comment as @MG_Bautista
0

This should do the trick for you.

$(document).ready(function () {
    $('#selectall').click(function () {
         var checked = $(this).is(':checked');            
        $('.selectedId').each(function () {
            var checkBox = $(this);               
            if (checked) {
                checkBox.prop('checked', true);                
            }
            else {
                checkBox.prop('checked', false);                
            }
        });

    });
});

JsFiddle http://jsfiddle.net/Ra3uL/

1 Comment

ops, simple but not "unselect" the selectall-checkbox when another item is "deselected". See @letiagoalves solution.
0

Assume parent checkbox has a class of mainselect and all child have class of childselect

Then selecting parent will select all checkbox and vice-versa with the code below:

$('#mainselect').live('change',function(){
if($(this).attr('checked') == 'checked') {
    $('.childselect').attr('checked','checked');
} else {
    $('.childselect').removeAttr('checked');
}
});

Comments

0

$(function(){

// add multiple select / deselect functionality
$("#selectall").click(function () {
      $('.case').attr('checked', this.checked);
});

// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){

    if($(".case").length == $(".case:checked").length) {
        $("#selectall").attr("checked", "checked");
    } else {
        $("#selectall").removeAttr("checked");
    }

});

});

Comments

0

A small enhancement to @letiagoalves solution, if your checkboxes have specific processing associated with a click event. Not asked for by the OP, but something I've had to do in the past. Basically, instead of directly setting the checked property of each dependent checkbox, it compares the checked state of each dependent checkbox to the selectall checkbox, and triggers a click event on those that are in the opposite state.

$('#selectall').click(function () {
    var parentState = $(this).prop('checked');
    $('.selectedId').each(function() {
        if ($(this).prop('checked') !== parentState) {
            $(this).trigger("click");
        }
});
});

DEMO: https://jsfiddle.net/jajntuqb/2/

Comments

0

You can simply add the handle function to the click event on the select all checkbox:

$('#chk-all-items').click(selectAll);

And then this function should be enough to select all or unselect all.

selectAll = function (event) {
        var self = this;
        $('.template-item-select').each(function () {
            this.checked = self.checked;
        });
    }

Comments

0

More specific solution:

$(document).ready(function(){
  $('#checkAll').on('click', function ( e ) {
    $('input[name="chck_box[]"]').prop('checked', $(this).is(':checked'));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <th><input type="checkbox" id="checkAll"></th>
    <th>Name</th>
    <th>Last</th>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" name="chck_box[]" value="1"></td>
      <td>Joe</td>
      <td>Doe</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="chck_box[]" value="2"></td>
      <td>John</td>
      <td>Smith</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="chck_box[]" value="3"></td>
      <td>Bill</td>
      <td>White</td>
    </tr>
  </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.