Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I need some help here,

I have 2 checkboxlist which the 2 checkboxlist items are populated using a stored procedure. However I added a "Select All" list item to the checkbox list.

Why does does it not work? when I checked a item in checkbox1 it did not enable the specific items I set in my codes.

Taking the below as an example scenario:

e.g in checkboxlist1 there is colours, food and drinks populated with the select all as well and in checkboxlist2 there is red, blue, green, chicken, spinach, coke and juice populated.

If I checked only drinks in checkboxlist1 so it should only enable coke and juice which the rest of the list items should be disabled in checkboxlist2, lets say I add to checked 'Food' it should enable Chicken and spinach in in checkboxlist2 which means coke, juice, chicken and spinach will be enabled when I checked both.


ASP.NET
CheckBoxList ID="Checkboxlist1" runat="server" Height="80px" Width="500px" AppendDataBoundItems="True" ViewStateMode="Enabled">
                    <asp:ListItem Text="Select All" Value="Select All"></asp:ListItem>
                </asp:CheckBoxList>
 <asp:CheckBoxList ID="Checkboxlist2" runat="server" Height="80px" Width="500px" AppendDataBoundItems="True" ViewStateMode="Enabled">
                    <asp:ListItem Text="Select All" Value="Select All"></asp:ListItem>
                </asp:CheckBoxList>


C#
Checkboxlist1.DataSource = ds.Tables[0];
               Checkboxlist1.DataTextField = ds.Tables[0].Columns["Column1"].ToString();
               Checkboxlist1.DataValueField = ds.Tables[0].Columns["Column1"].ToString();
               Checkboxlist1.DataBind();
               Checkboxlist2.DataSource = ds.Tables[1];
               Checkboxlist2.DataTextField = ds.Tables[1].Columns["Column2"].ToString();
               Checkboxlist2.DataValueField = ds.Tables[1].Columns["Column2"].ToString();
               Checkboxlist2.DataBind();


JavaScript
$(function () {
    $("#Checkboxlist1 :checkbox").change(function () {
            var ischecked = $(this).is(":checked");
            var val = $(this).val();
            //alert(val);
            if (val == "Select All") {
                if (ischecked) {
                    $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
                    $(this).removeAttr('disabled');
                    $("#Checkboxlist1  :checkbox").prop("checked", false);
                    $(this).prop("checked", true);
                    $("#Checkboxlist2 :checkbox").removeAttr('disabled');

                    return;
                } else {
                    $("#Checkboxlist1 :checkbox").removeAttr('disabled');
                    $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
                    return;
                }
            } else if (val != "Select All") {
                if (ischecked) {
                    $("#Checkboxlist1 :checkbox[value='Select All']").prop("checked", false);
                }
            } else if (val == "Food") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox[value='Chicken']").removeAttr('disabled');
                    $("#Checkboxlist2 :checkbox[value=spinach]").removeAttr('disabled');

                } else {
                    $("#Checkboxlist2 :checkbox[value='Chicken']").attr("disabled", "disabled");
                  $("#Checkboxlist2 :checkbox[value='Spinach']").attr("disabled", "disabled");


                }
            } else if (val == "Drinks") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox[value='Coke']").removeAttr('disabled');
                    $("#Checkboxlist2 :checkbox[value='Juice']").removeAttr('disabled');

                } else {
                    $("#Checkboxlist2 :checkbox[value='Coke']").attr("disabled", "disabled");
                    $("#Checkboxlist2 :checkbox[value=Juice]").attr("disabled", "disabled");
                }
            }
        });
    });


$(function () {
    $("#Checkboxlist2 :checkbox").change(function () {
            var ischecked = $(this).is(":checked");
            var val = $(this).val();
            //alert(val);
            if (val == "Select All") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
                    $(this).removeAttr('disabled');
                    $("#Checkboxlist2  :checkbox").prop("checked", false);
                    $(this).prop("checked", true);

                    return;
                } else {
                    $("#Checkboxlist2  :checkbox").removeAttr('disabled');
                    return;
                }
            }
        });
    });
Posted
Updated 24-Sep-15 3:32am
v5

1 solution

Firstly, remove the loop inside the change event as its resetting the ischecked value each time and use it like:

JavaScript
$("#Checkboxlist1 :checkbox").change(function () {
    var ischecked = $(this).is(":checked");
    var val = $(this).val();
    //alert(val);
    if (val == "Select All") {
        if (ischecked) {
            $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
            $(this).removeAttr('disabled');
            $("#Checkboxlist1  :checkbox").prop("checked", false);
            $(this).prop("checked", true);
            $("#Checkboxlist2 :checkbox").removeAttr('disabled');

            return;
        } else {
            $("#Checkboxlist1 :checkbox").removeAttr('disabled');
            $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
            return;
        }
    } else if (val != "Select All") {
        if (ischecked) {
            $("#Checkboxlist1 :checkbox[value='Select All']").prop("checked", false);
        }
    } else if (val == "Food") {
        if (ischecked) {
            $("#Checkboxlist2 :checkbox[value='Chicken']").removeAttr('disabled');
            $("#Checkboxlist2 :checkbox[value=spinach]").removeAttr('disabled');

        } else {
            $("#Checkboxlist2 :checkbox[value='Chicken']").attr("disabled", "disabled");
            $("#Checkboxlist2 :checkbox[value='Spinach']").attr("disabled", "disabled");


        }
    } else if (val == "Drinks") {
        if (ischecked) {
            $("#Checkboxlist2 :checkbox[value='Coke']").removeAttr('disabled');
            $("#Checkboxlist2 :checkbox[value='Juice']").removeAttr('disabled');

        } else {
            $("#Checkboxlist2 :checkbox[value='Coke']").attr("disabled", "disabled");
            $("#Checkboxlist2 :checkbox[value=Juice]").attr("disabled", "disabled");
        }
    }
});



Second you can can't have a one click event inside a change event. So, just use the change event and use all the code inside the click event like:-

C#
$("#Checkboxlist2 :checkbox").change(function () {
    var ischecked = $(this).is(":checked");
    var val = $(this).val();
    //alert(val);
    if (val == "Select All") {
        if (ischecked) {
            $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
            $(this).removeAttr('disabled');
            $("#Checkboxlist2  :checkbox").prop("checked", false);
            $(this).prop("checked", true);

            return;
        } else {
            $("#Checkboxlist2  :checkbox").removeAttr('disabled');
            return;
        }
    }
});


Also, there is no need to have multiple document ready event handler $(function () {, just use one and put the above codes inside it. It will just work fine.

Let me know if you are still facing any issue in it.
 
Share this answer
 
v2
Comments
F-ES Sitecore 24-Sep-15 9:32am    
I've already said most of this. This is the problem when people keep asking the same question over and over (this is about the 4th time he has posted this), it just ends up wasting people's time. What the OP really wants is for someone to just give him the code and he is going to keep asking the same question over and over until he gets it.
waynetan123 24-Sep-15 9:37am    
Hi there I'm sincerely asking a question after trying to code multiple times. Its not like I did not try coding. My code logic is right just that im unsure why am I facing the error.
Richard Deeming 24-Sep-15 10:42am    
Don't forget you can flag the question as a repost.
waynetan123 24-Sep-15 9:36am    
Hi, I have removed the loop and the .click as have updated the codes in my question but it still does not work. (e.g I check on 'Food' in checkboxlist1 it does not remove the disable in 'spinach' and 'chicken' in checkboxlist2
Palash Mondal_ 24-Sep-15 9:47am    
Are you getting correct value using alert(val); when a item inside Checkboxlist1 is changed?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900