public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Archive for May, 2010

Check/Uncheck All Checkboxes with JQuery

Posted by Virendra Dugar on May 23, 2010

This particular article is about a pretty common functionality which I think most of the software engineers face in their projects. Even I came across the same situation many times. Previously I used to achieve the functionality via JavaScript, which really worked flawlessly. But as a software engineer, I think one should always adapt the new technology. So, this time I tried to achieve the same via JQuery as its booming now and one need to upgrade himself. So what functionality are we talking about? See below given images.

Okay. So you must have got the idea about the functionality. Yes, you guessed it right. It’s about selecting all the items of checkbox list, when select all is checked. You can find thousands of solution using Jquery when you google it. Then why you are here. Well, in fact I googled a lot but didn’t even come across to a single article which solves my purpose. Let me explain the requirement of the functionality here.

  1. When “Select All” checkbox is checked, it must change the status of all the items of the checkbox list as per “Select All” status. For e.g., if “Select All” is true then all the items of the checkbox list must be set to true or vice versa.
  2. Assume, “Select All” is true and all the items of the checkbox list are also true. But when I uncheck any item of checkbox list, it must also uncheck the select all button.
  3. Assume, “Select All” is unchecked. And one of the items in the checkbox list is also unchecked. But now when I check the checkbox list item, then “Select All” must be checked. As all the items in the checkbox list are checked.

So let’s go directly to the code.

Prerequisite: I assume that you know the basics of JQuery. Read this article for information on Introduction to JQuery.

<table cellpadding="2" cellspacing="2" width="50%">
            <tr>
                <th align="left" style="font-family:Arial;">
                    <h3>
                        Checkbox Select All Demo using JQuery</h3>
                </th>
            </tr>
            <tr>
                <td>
                    &nbsp;<asp:CheckBox ID="chkSelectAll" runat="server" Text="Select All" Font-Size="9pt"
                        Font-Names="Arial" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:CheckBoxList ID="chkItems" runat="server" Width="200px" Font-Size="9pt" Font-Names="Arial"
                        BorderStyle="Solid" BorderColor="Black" BorderWidth="1px">
                    </asp:CheckBoxList>
                </td>
            </tr>
        </table>

Well, I have placed a checkbox “Select All” with ID “chkSelectAll” and a checkbox list with ID “chkItems”.

<script type="text/javascript">
    $(document).ready(function() {
        $("#<%=chkSelectAll.ClientID %>").click(function() {
                $("#<%= chkItems.ClientID %> input:checkbox").attr('checked',this.checked);
        });

        $("#<%=chkItems.ClientID %> input:checkbox").click(function(){
            if($("#<%= chkSelectAll.ClientID %>").attr('checked') == true && this.checked == false)
                $("#<%= chkSelectAll.ClientID %>").attr('checked',false);

             if(this.checked == true)
                CheckSelectAll();
        });

        function CheckSelectAll()
        {
            var flag = true;
            $("#<%=chkItems.ClientID %> input:checkbox").each(function() {
                if(this.checked == false)
                    flag = false;
            });
              $("#<%= chkSelectAll.ClientID %>").attr('checked',flag);
        }

    });

    </script>

Well above given code solves my purpose. Let’s go through the code function by function.

$("#<%=chkSelectAll.ClientID %>").click(function() {
                $("#<%= chkItems.ClientID %> input:checkbox").attr('checked',this.checked);
        });

This code binds click event to “Select All” checkbox. It will search for all the checkbox inside the chkItems (as checkbox list, when rendered becomes a table. See the view source.) and set the checked attribute to the value of “Select All” checkbox. 2 Lines of code can serve your purpose. That’s why I love JQuery.

Next function binds the click event to every checkbox of checkbox list.

$("#<%=chkItems.ClientID %> input:checkbox").click(function(){

Inside the function, it first check for the status of “Select All”. If it’s true and the clicked checkbox is unchecked (see Image 3), then it will uncheck the “Select All” checkbox.

 if($("#<%= chkSelectAll.ClientID %>").attr('checked') == true && this.checked == false)
                $("#<%= chkSelectAll.ClientID %>").attr('checked',false);

Next task is, if the clicked checkbox item status is checked, then we need to check the status of all the items of checkbox list and if every item is checked, then we must check the “Select All” checkbox.

 if(this.checked == true)
                CheckSelectAll();

This will call a function “CheckSelectAll”. This function iterates thorugh the checkbox list and finds all the items are checked or not. If yes, then it sets the status of “Select All” checkbox accordingly.

 function CheckSelectAll()
        {
            var flag = true;
            $("#<%=chkItems.ClientID %> input:checkbox").each(function() {
                if(this.checked == false)
                    flag = false;
            });
              $("#<%= chkSelectAll.ClientID %>").attr('checked',flag);
        }

each() method of JQuery will perfomes the iteration for specified item.

That’s it. Hope this will help you in your work.

Enjoy,
Virendra

Advertisements

Posted in jQuery | Tagged: | Leave a Comment »

 
%d bloggers like this: