Select and Deselect all option in checkbox list using javascript


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckAllCheckBox.aspx.cs" Inherits="WebApplication2.CheckAllCheckBox" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script language="javascript"> function CheckAll() { var intIndex = 0; var rowCount = document.getElementById('cbList').getElementsByTagName("input").length; for (i = 0; i < rowCount; i++) { if (document.getElementById('cbAll').checked == true) { if (document.getElementById("cbList" + "_" + i)) { if (document.getElementById("cbList" + "_" + i).disabled != true) document.getElementById("cbList" + "_" + i).checked = true; } } else { if (document.getElementById("cbList" + "_" + i)) { if (document.getElementById("cbList" + "_" + i).disabled != true) document.getElementById("cbList" + "_" + i).checked = false; } } } } function UnCheckAll() { var intIndex = 0; var flag = 0; var rowCount = document.getElementById('cbList').getElementsByTagName("input").length; for (i = 0; i < rowCount; i++) { if (document.getElementById("cbList" + "_" + i)) { if (document.getElementById("cbList" + "_" + i).checked == true) { flag = 1; } else { flag = 0; break; } } } if (flag == 0) document.getElementById('cbAll').checked = false; else document.getElementById('cbAll').checked = true; } </script> </head> <body> <form id="form1" runat="server"> <div> <div style="border: 1px solid grey; width: 100px"> <asp:CheckBox ID="cbAll" runat="server" Text="Select All" onclick="CheckAll();" BackColor="Aqua" /> <asp:CheckBoxList ID="cbList" runat="server" ValidationGroup="VGroup" onclick="UnCheckAll();"> <asp:ListItem Value="1">Asp.net</asp:ListItem> <asp:ListItem Value="2">C#.net</asp:ListItem> <asp:ListItem Value="3">Vb.net</asp:ListItem> <asp:ListItem Value="4">Oracle</asp:ListItem> <asp:ListItem Value="5">SqlServer</asp:ListItem> <asp:ListItem Value="6">Sharepoint</asp:ListItem> </asp:CheckBoxList> </div> </div> </form> </body> </html>