Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,
How to check/uncheck checkboxes(Child) using a single(Parent) checkbox or vice-versa?
When i am using normal Html checkbox following code is working.
where Child and Parent are classes given to checkboxes.

C#
// add multiple Check / Uncheck functionality
    $(".Parent").click(function () {
          $(".Child").attr("checked", this.checked);
    });

    // if all child checkbox are selected, check the Parent checkbox
    // and viceversa
    $(".Child").click(function(){

        if($(".Child").length == $(".Child:checked").length) {
            $(".Parent").attr("checked", "checked");
        } else {
            $(".Parent").removeAttr("checked");
        }
  });



But when i am using asp.net checkboxes this code is not working.
then i have modified the code as below.
C#
// add multiple Check / Uncheck functionality
  //Parent class is having only one checkbox 
$(".Parent:first-child").click(
function() {
          $(".Child").find(":checkbox").attr("checked", this.checked);
    });

    // if all child checkbox are selected, check the Parent checkbox
    // and viceversa
    $(".Child").find(":checkbox").click(function(){

        if($(".Child").length == $(".Child:checked").length) {
            $(".Parent").find(":checkbox").attr("checked", "checked");
        } else {
            $(".Parent").find(":checkbox").removeAttr("checked");
        }
  });




Is this Because of Span Rendering Problem?
How to Solve this Problem?

Thanks in Advance.
Posted
Comments
user 3008 26-May-14 0:49am    
Here the checkboxes have parent child relationship thus you have give as traversing.If suppose the two checkboxes do not have parentchild relationship and are two different checkboxes with different class name and Id then how can we give the click function?

Try this code. Will help you.
JavaScript
$(document).ready(function(){
    $('.check:button').toggle(function(){
        $('input:checkbox').attr('checked','checked');
        $(this).val('uncheck all')
    },function(){
        $('input:checkbox').removeAttr('checked');
        $(this).val('check all');        
    })
})
 
Share this answer
 
v2
Just give class to asp.net check boxes and it must work.
 
Share this answer
 
You are doing nothing wrong, but you should always use 'prop()' instead of 'attr()' when you are dealing with such check & uncheck.

Please modified your code and use prop() in place of attr() for selecting all checkbox having class child.
JavaScript
$(".Child").find(":checkbox").prop("checked", true);


And do same for selecting and deselecting the Parent checkbox respectively.

JavaScript
$(".Parent").find(":checkbox").prop("checked", true);
$(".Parent").find(":checkbox").prop("checked", false);


hope this will help you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900