3

I have an ASP.NET CheckBoxList:

<asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="10%">
    <asp:ListItem Selected="True" Value="1">White</asp:ListItem>
    <asp:ListItem Selected="False" Value="2">Black</asp:ListItem>
    <asp:ListItem Value="3">Red</asp:ListItem>
    <asp:ListItem Value="4">Green</asp:ListItem>
    <asp:ListItem Value="5">Blue</asp:ListItem>
</asp:CheckBoxList>

I need to modify/delete a particular label of a particular CheckBox in a CheckBoxList:

The following code is working fine for the CheckBox value, however it is not working for the CheckBox label.

var CheckBoxListInputValue = 3; //value may be dynamic
var CheckBoxListInputInnerHTML = "Red"; //value may be dynamic
$("#CheckBoxList1 label[innerHTML =" + CheckBoxListInputInnerHTML + "]").remove(); //Not working
$("#CheckBoxList1 :input[value = " + CheckBoxListInputValue + "]").remove(); //Working

BTW, I don't want to use any for loop.

2 Answers 2

1

asp.net generate a table from your checkboxlist, and in order to remove an option you must remove actually a TR. The following code remove the Red option on your sample then you can change Red with the dynamic value

  $('#CheckBoxList1 label').each(function () {
        if ($(this).text() == 'Red') {
            $(this).closest('tr').remove();
            return false;
        }
    });

Test it here

Sign up to request clarification or add additional context in comments.

Comments

0

you can use :contains selector:

$("#CheckBoxList1 label:contains(" + CheckBoxListInputInnerHTML + ")").remove(); 

please note that :input selector is deprecated, you can use input instead.

Comments

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.