-1

I have CheckBoxList when I have one default value called "Select All" and rest items binded from database:

<asp:CheckBoxList runat="server" ID="chkBxLstSystemTypes" CssClass="chkBxList" RepeatDirection="Horizontal" RepeatColumns="10" TabIndex="2">
<asp:ListItem Value="SelectAll">Select All</asp:ListItem>

I would like to have JavaScript or jQuery function which will check all check boxes when user clicks "Select all" and deselect the check boxes when user deselects the "Select All".

-- EDIT 1 Rendered HTML:

<table id="ctl00_MainContent_chkBxLstSystemTypes" class="chkBxList" border="0">
  <tr>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_0" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$0" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_0">Select All</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_1" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$1" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_1">ASG</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_2" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$2" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_2">AVP</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_3" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$3" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_3">CDR</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_4" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$4" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_4">CMS</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_5" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$5" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_5">CUCM_Logins</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_6" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$6" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_6">CUCM_Subscribers</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_7" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$7" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_7">Cybertech</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_8" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$8" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_8">eHealth</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_9" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$9" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_9">Intuity</label>
    </td>
  </tr>
  <tr>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_10" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$10" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_10">MMSMailbox</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_11" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$11" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_11">MMSUser</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_12" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$12" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_12">NICE Perform</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_13" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$13" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_13">PBX</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_14" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$14" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_14">UNITY</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_15" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$15" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_15">VoIP</label>
    </td>
    <td>
      <input id="ctl00_MainContent_chkBxLstSystemTypes_16" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$16" tabindex="2" />
      <label for="ctl00_MainContent_chkBxLstSystemTypes_16">WebEx</label>
    </td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
ironcurtain
  • 650
  • 9
  • 35

2 Answers2

0

I suggest to use CliendIdMode="static" in your runat="server" elements that you use them in js. You can try this:

$("#ctl00_MainContent_chkBxLstSystemTypes_0").click(function () {
    $(this).parents("table").find(":checkbox").prop("checked", $(this).prop("checked"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="ctl00_MainContent_chkBxLstSystemTypes" class="chkBxList" border="0">
    <tr>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_0" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$0" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_0">Select All</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_1" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$1" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_1">ASG</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_2" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$2" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_2">AVP</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_3" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$3" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_3">CDR</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_4" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$4" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_4">CMS</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_5" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$5" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_5">CUCM_Logins</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_6" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$6" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_6">CUCM_Subscribers</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_7" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$7" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_7">Cybertech</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_8" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$8" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_8">eHealth</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_9" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$9" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_9">Intuity</label>
        </td>
    </tr>
    <tr>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_10" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$10" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_10">MMSMailbox</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_11" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$11" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_11">MMSUser</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_12" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$12" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_12">NICE Perform</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_13" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$13" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_13">PBX</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_14" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$14" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_14">UNITY</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_15" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$15" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_15">VoIP</label>
        </td>
        <td>
            <input id="ctl00_MainContent_chkBxLstSystemTypes_16" type="checkbox" name="ctl00$MainContent$chkBxLstSystemTypes$16" tabindex="2" />
            <label for="ctl00_MainContent_chkBxLstSystemTypes_16">WebEx</label>
        </td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>
user2019037
  • 764
  • 1
  • 7
  • 14
  • This is what I was looking for. The one problem is that I have threee diferent sets of the CheckBoxLists and when I click "Select All" on first CheckBoxList, all other are checked. How can I use this only to this first CheckBoxList? – ironcurtain Oct 20 '14 at 17:23
  • Then you have to specify the container of the checkboxes. Assuming checkboxes belongs to different tables. Check updated answer. – user2019037 Oct 20 '14 at 17:29
  • 1
    Your update works! Thanks so much. I am beginning working with jQuery and now it is a nightmare for me:( From what I understand in second line you are looking for parent of #ctl00_MainContent_chkBxLstSystemTypes_0 which are type of "checkbox" but what does the ":" mean? – ironcurtain Oct 20 '14 at 17:35
  • You are welcome. Is [checkbox](http://api.jquery.com/checkbox-selector/) selector for jquery. – user2019037 Oct 20 '14 at 17:40
0

Add this jQuery code & you are done.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$('.chkBxList input:checkbox:first').change(function(){
  var status = $(this).is(':checked');
  $('.chkBxList input:checkbox').prop('checked', status);
});
</script>

Jsfiddle: http://jsfiddle.net/y9dhwx72/

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30