check/uncehck checkboxlist with jquery
<p>most examples i have seen online have a snippet where they use 2 checkboxes, one for just one checkbox (all) and the second is the checkboxlist. In myy case i have only one checkboxlist bound to datasource, example my datasource list options are(All,apple,orange,red,blue), I got most of it working except when everything is unchecked and i check the last item, example blue, it checks the ALL option. so not working properly. id is the id of the 'All' item in the list</p>

<pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
    &lt;script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
 $(document).ready(function () {
     var id = "#&lt;%=cbOptions.ClientID %&gt;_0";
     var checkboxlistid = "#&lt;%= cbOptions.ClientID %&gt;";
     $(id).click(function () {
         $("#&lt;%= cbOptions.ClientID %&gt; input:checkbox").attr('checked', this.checked);
     });
     $(checkboxlistid +  " input:checkbox").click(function () {
         if ($(checkboxlistid).attr('value') != 0) {
             if ($(id).attr('checked') == true &amp;&amp; this.checked == false) {
                 $(id).attr('checked', false);
             }
             else {
                 if ($(id).attr('checked') == true &amp;&amp; this.checked == true)
                     CheckSelectAll();
             }

         }
     });
     function CheckSelectAll() {
         var flag = true;
         $(checkboxlistid + " input:checkbox").each(function () {
             if ($(checkboxlistid).attr('value') != 0) {
                 if (this.checked == false) {
                     flag = false;
                 }
                 else {
                     if ($(id).attr('checked') == true &amp;&amp; this.checked == false) {
                         flag = false;
                     }
                     else {
                         flag = true;
                     }
                 }
             }
         });

         $(id).attr('checked', flag);
     }
      });    
         &lt;/script&gt;
                &lt;asp:CheckBoxList runat="server" ID="cbOptions" &gt;
                &lt;/asp:CheckBoxList&gt;
        &lt;/asp:Content&gt;
</code></pre>
