Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ASP.NET find a particular CheckBox in a CheckBoxList by the innerHTML of the CheckBox Label

Tags:

jquery

asp.net

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.

like image 513
user1438964 Avatar asked Nov 18 '25 23:11

user1438964


1 Answers

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

like image 104
Frenchi In LA Avatar answered Nov 21 '25 12:11

Frenchi In LA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!