Thursday, June 28, 2012

Check/Uncheck all checkboxes in Jquery

This is simple, but want to show you how to get it working in simple and efficient way. Usually we have a parent check box and then some child checkboxes. Depends on the parent check box selection, all the child checkboxes should behave exactly same. So, below is the Jquery function which will do that magic for you.

  1. function CheckUncheckAllCheckBoxes(objID, checkedValue) {  
  2. if (objID == null || objID == undefined )  
  3. return;  
  4.   
  5. $(objID + " input[type=checkbox]").each(function() {  
  6. this.checked = checkedValue;  
  7. });  
  8. }  
If you observe, I am using two parameters for the function named objID and checkedValue. objID is the parameter for knowing which checkbox group or list we need to check or uncheck? Like, on a page we may have many checkboxes and groups or lists. So, we need a way to find out which group or list we need to check or uncheck? For this reason I added a parameter for the function to check only the checkboxes under that ID or Class. Possible values for the objID are
  1. #parent Control ID of the element which has the check boxes declared. Example: #ageList
  2. .parent control class of the element under which all the check boxes defined. Example: .edit
And second parameter, it is for passing the parent selected check box value. If it is ON, then logic will loop through and set the each checkbox to checked otherwise unchecked.

Usage - How to call this method:
Code for check/Uncheck all in ASP.NET

  1. $("#<%=cbAllStates.ClientID %>").click(function() {  
  2.    CheckUncheckAllCheckBoxes("#<%=cblState.ClientID %>"this.checked);  
  3. });  
Note: cblAllStates is the parent ASP.NET check box which is controlling child. cblState is the asp.net checkboxlist and in our terms child check boxes.

Code for Check/Uncheck all in HTML:

  1. $("#cbAllStates").click(function() {  
  2.    CheckUncheckAllCheckBoxes("#divChilds"this.checked);  
  3. });  
Note: cbAllStates is the parent check box control and divChilds is the division <DIV< tag which has all the child check boxes present in it.

**UPDATED** 06/22/2010
This is the small update for the check/uncheck the all check box depends on the child check box selection. If any of the child check box is unchecked or the all child check boxes are selected then the all check box will toggle depends on it. Below is the work around.

Example:
Code for  Toggling the all check box depends on the child check boxes:

  1. function ToggleSelectAllCheckBox(allCheckBox, checkedValue, obj) {  
  2.     if (allCheckBox == null || allCheckBox == undefined)  
  3.         return;  
  4.     if (!checkedValue)   
  5.         $(allCheckBox).attr("checked"false);  
  6.     else {  
  7.         var areAllChecked = true;  
  8.         $(obj + " input[type=checkbox]").each(function() {  
  9.             if (!this.checked) {  
  10.                 areAllChecked = false;                  
  11.             }  
  12.         });  
  13.         $(allCheckBox).attr("checked", areAllChecked);  
  14.     }  
  15. }  
In the above function param1 is the all child check boxes, param2 is the current child check box selection and param3 is the all check box selector.

How to use:
ASP.NET Checkbox list:

  1. $("#<%=cblAge.ClientID %> input[type=checkbox]").click(function() {  
  2.                 ToggleSelectAllCheckBox("#<%=cbAllAges.ClientID %>"this.checked, "#<%=cblAge.ClientID %>");  
  3.  });  
Note: cblAges is the check box list id and the cblAge is the all check box for the age group. So, the click event is for the all check boxes inside the check box list.

HTML Check boxes:

  1. $(".ageGroup  input[type=checkbox]").click(function() {  
  2.                 ToggleSelectAllCheckBox(".ageGroup"this.checked,  "#allAgeCheckbox");  
  3.  });  
Note: ".ageGroup" is the div or some parent element which holds all the checkboxes in HTML. "#allAgeCheckbox" is the id of the all checkbox for that age group.

No comments:

Post a Comment