Imad Imad - 3 days ago 4
jQuery Question

How to get checkbox whose label value is known

I have some checkboxes that has UNIQUE values for their labels. I know the label value, how can I access checkbox.

Eg:

<input id="id1" type="checkbox">
<label for="id1">D2_Telecaller</label>


How to get this checkbox if I know D2_Telecaller?

Answer

I have done it like below, this will work well when checkbox labels are unique, and of course they will be ;)

    // get table that has checkboxes
    checkBoxList = $("[id*=tblRoleList]");
    // Find all labels
    checkBoxLabels = checkBoxList.find("label");
    // Loop through all lables
    checkBoxLabels.each(function () {
    // If label text matched
    if ($.inArray($(this).text(), "D2_Telecaller") != "-1") {
       // Previous input must be associated checkbox
       chk = $(this).prev();
    }
    });
Comments