1

I have a asp.net checkboxlist as follows:

<asp:CheckBoxList ID="cblCurrency" runat="server">
    <asp:ListItem Text="ALL" Value="0"></asp:ListItem>
    <asp:ListItem Text="A" Value="1"></asp:ListItem>
    <asp:ListItem Text="B" Value="2"></asp:ListItem>
    <asp:ListItem Text="C" Value="3"></asp:ListItem>
    <asp:ListItem Text="D" Value="4"></asp:ListItem>
</asp:CheckBoxList>

The requirement is such that if "ALL" is selected it should remove checked property from all other checkboxes and if any other checkbox is selected it should remove checked property of "ALL". I want to achieve this using javascript but most preferably in jQuery. Please reply if you know how to do it. Help would be appreciated.

3
  • What is "ALL"? Is it another checkbox? Commented Jul 20, 2013 at 1:36
  • Yes. it is the first checkbox as shown above. Commented Jul 20, 2013 at 2:19
  • Can you put "ALL" as a separate check-box? out side cblCurrency with a different ID? Commented Jul 20, 2013 at 2:21

3 Answers 3

1

Here's my solution:

$("#<%=cblCurrency.ClientID%> input").click(function () {
    if ($('<%= "#" + cblCurrency.ClientID + "_0" %>').is(":checked")) {
        $("#<%=cblCurrency.ClientID%> input:checkbox").not(this).prop("checked", false);
    }
});

Basically, we utilize the index of the check-box which is generated by ASP.NET and used in the ID attribute at the end. Like this one: cblCurrency_1 which means the check-box at index 1.

0
0

Try this:

Markup:

<asp:CheckBoxList ID="cblCurrency" runat="server" CssClass="CheckBoxList" ClientIDMode="Static">
    <asp:ListItem Text="ALL" Value="0"></asp:ListItem>
    <asp:ListItem Text="A" Value="1"></asp:ListItem>
    <asp:ListItem Text="B" Value="2"></asp:ListItem>
    <asp:ListItem Text="C" Value="3"></asp:ListItem>
    <asp:ListItem Text="D" Value="4"></asp:ListItem>
</asp:CheckBoxList>

JavaScript:

$(document).ready(function () {
    $(".CheckBoxList input:checkbox:first").change(function () {
        $(".CheckBoxList").find(':checkbox').prop("checked", this.checked);
    });

    $(".CheckBoxList input:checkbox:not(first)").change(function () {
        $(".CheckBoxList").find(':checkbox:first').prop("checked", this.checked);
    });
});
2
  • Thanks for this code. However this does not seem to work when selecting "ALL". It selects all checkboxes. Commented Jul 20, 2013 at 5:39
  • Oh okay, I misread your requirement. I am curious as to why you would not want ALL to actually check them all, that is generally how most user interfaces work (i.e. GMail select all messages). If you do want that type of interaction you described, then you are better off making a mutually exclusive control like radio buttons to have the user decide if they want all or not, if they choose they do not want all then it would show the checkboxes to let them decide which they want or do not want. I would think your user interface was broken if I was a new user of your system. Just my two cents. Commented Jul 20, 2013 at 12:29
0

Create an event handler for managing clicks on all of the check boxes, and capture the user behavior you want.

If you take this code, and view source on the HTML that is rendered, we get:

    <table id="MainContent_cblCurrency">
    <tr>
        <td><input id="MainContent_cblCurrency_0" type="checkbox" name="ctl00$MainContent$cblCurrency$0" value="0" /><label for="MainContent_cblCurrency_0">ALL</label></td>
    </tr><tr>
        <td><input id="MainContent_cblCurrency_1" type="checkbox" name="ctl00$MainContent$cblCurrency$1" value="1" /><label for="MainContent_cblCurrency_1">A</label></td>
    </tr><tr>
        <td><input id="MainContent_cblCurrency_2" type="checkbox" name="ctl00$MainContent$cblCurrency$2" value="2" /><label for="MainContent_cblCurrency_2">B</label></td>
    </tr><tr>
        <td><input id="MainContent_cblCurrency_3" type="checkbox" name="ctl00$MainContent$cblCurrency$3" value="3" /><label for="MainContent_cblCurrency_3">C</label></td>
    </tr><tr>
        <td><input id="MainContent_cblCurrency_4" type="checkbox" name="ctl00$MainContent$cblCurrency$4" value="4" /><label for="MainContent_cblCurrency_4">D</label></td>
    </tr>
</table>

It's not pretty, but it is what we have to work with.

This JS will capture the click of all the checkboxes. It locates the checkboxes based on the ID of the containing table. If it is the "all" checkbox is checked(based on the value of the checked input) it will uncheck all currently selected checkboxes:

$().ready(
        function () {
            $("#<%=cblCurrency.ClientID%> input").click( // The ClientID property should tell you that the ID if the table is  
                function () {
                    var eventSource = $(this); //The checkbox that was clicked
                    //for debugging Delete once you get working                    
                    //console.log(eventSource.val())
                    //console.log(eventSource.is(':checked'))

                    if (eventSource.val() == "0" && eventSource.is(':checked')) { // Make sure the value is what you expect and that the check box is being checked (and not unchecked)
                        $("#<%=cblCurrency.ClientID%> input").not(eventSource).prop('checked', false)// uncheck all other checkbox. exclude the input that is the source of the event.

                    }
                }
            )
        }
            );

A version of this is here: http://jsfiddle.net/RGW4Q/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.