3

Possible Duplicate:
Javascript or Jquery to check and uncheck all checkbox

I have 12 asp:checkboxes on a page and I would like to add one which selects/deselects all of them. What's the best way to do that?

I've seen people use jquery but I'm not very familiar with JS. IS there a generic jquery finction to use?

<head>...
   <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
...</head>

<body>...
    <script type="text/javascript">
        function Selectall() {
            if ($('.JchkAll').is(':checked')) {
                // .JchkGrid cssClass will be assigned to all other checkboxes in your control 
                $('.JchkGrid').attr('checked', 'true');
            }
            else {
                $('.JchkGrid').removeAttr('checked', 'false');
            }
        } 
    </script>
<form>...
<asp:CheckBox runat="server" ID="cbSelectAll" Text="Select/Deselect All" CssClass="JchkAll" onchange="Selectall();"/>
        <asp:CheckBox runat="server" ID="cbName" Text="Name" class="JchkGrid"/>
        <asp:CheckBox runat="server" ID="cbModel" Text="Model" CssClass="JchkGrid"/>
...</form>
...</body>
Community
  • 1
  • 1
user1468537
  • 693
  • 4
  • 13
  • 27

5 Answers5

3

Give your chckbox a css class and try this way

// .JchkAll is cssclass of your checkbox on which's click you need to check all Checkboxes

function Selectall() {
  if ($('.JchkAll').is(':checked')) {
   // .JchkGrid cssClass will be assigned to all other checkboxes in your control
    $('.JchkGrid').attr('checked', 'true');
  }
  else {
    $('.JchkGrid').removeAttr('checked', 'false');
  }
}

Edit:

Also Don't forget to add this on checkbox's onchange attribute..

<asp:CheckBox runat="server" onchange="Selectall();" ID="cbSelectAll" 
     Text="Select/Deselect All" CssClass="JchkAll"/>

But this will give you a compiler warning...it would be better if you add it from code behind on Page_Load event like

    cbSelectAll.Attributes.Add("onchange","Selectall");
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
2

First you find all the checkboxes, and then you change them. This you done on client via javascript. For example this is a function that if you call it is check all the chekboxes on a page.

function SwapCheck()
{   
    // find them
    var allChecks = jQuery('input[type=checkbox]');

    allChecks.each( function() {
        jQuery(this).prop("checked", !this.checked);
        // use this for jQuery ver 1.6 and before
        // jQuery(this).attr("checked", !this.checked);
    });     
}

If you like to eliminate some checkboxs then warp them with a span, or div and use the

jQuery('#DivIDWithChecksToSelect input[type=checkbox]');

If you like to check them all you can use this code

jQuery(this).attr("checked", "checked");

Also you can use the jQuery(this).prop("checked", false); to uncheck them.

relative:
Setting "checked" for a checkbox with jQuery?
Check all CheckBoxes in GridView

About the .attr() and the .prop() read here : http://api.jquery.com/prop/

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
1

Give your checkboxes one CssClass name (chk_group in my example), and the give your check box that will change all of them another CssClass name (chk_select_all in my example)

then you can try this jQuery code

​$(document).ready(function(){
    $(".chk_select_all input[type=checkbox]").click(function(){
       $(".chk_group input[type=checkbox]").attr('checked', this.checked); 
    });
 });​

Check here a working version: http://jsfiddle.net/a2XeK/

In ASP.NET WebForms, you'll have to use the selector .chk_select_all input[type=checkbox] because the rendering engine will create a with css style, and inside the span there will be the actual checkbox.

0

Multiple ways to do it. See which is best for you. Using JavaScript

function toggleCheckboxes(flag) {    
    var form = document.getElementById('groupImportForm');
    var inputs = form.elements;
    if(!inputs){
        //console.log("no inputs found");
        return;
    }
    if(!inputs.length){
        //console.log("only one elements, forcing into an array");
        inputs = new Array(inputs);        
    }

    for (var i = 0; i < inputs.length; i++) {  
      //console.log("checking input");
      if (inputs[i].type == "checkbox") {  
        inputs[i].checked = flag;
      }  
    }  
}

Hope this helps.

Peru
  • 2,871
  • 5
  • 37
  • 66
-2

You can do it in the codebehind if you want. In the OnCheckedChanged event of the main checkbox, set all of the Checked variables of the checkboxes to true.

jdesai927
  • 62
  • 1
  • 1
  • 5