Imad Imad - 1 year ago 87
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.


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

How to get this checkbox if I know D2_Telecaller?

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download