0

I'm trying to change the css of the following label using JQuery but I can't figure it out. This is actually the html from my checkboxlist (asp.net) and this is what I have so far. Can anybody please help? Thank you.

The JQuery below finds the entire table and makes all labels red, not just the one that I checked.

$("#CheckBoxList1").click(function() {
    $(this).find('label').removeClass('red');
    if ($('span').hasClass('checked'))
        $(this).find('label').addClass('red');
});

<table id="CheckBoxList1" border="0">
  <tbody>
    <tr>
      <td>
        <div class="checker" id="uniform-CheckBoxList1_0">
          <span>
            <input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" style="opacity: 0; "/>
          </span>
        </div>
        <label for="CheckBoxList1_0">Mark Park</label>
      </td>
      <td>
        <div class="checker" id="uniform-CheckBoxList1_1">
          <span>
            <input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" style="opacity: 0; "/>
          </span>
        </div>
        <label for="CheckBoxList1_1">Amy Lee</label>
      </td>
      <td/>
    </tr>
  </tbody>
</table>

3 Answers 3

1
$("#CheckBoxList1").click(function() {
    $(this).find('label').removeClass('red');
    $('span.checked').parent().next('label').addClass('red');
});
Sign up to request clarification or add additional context in comments.

Comments

0

Can you add an class/id to the labels and checkboxes and access it that way:

 $(".check").click(function() {
   var id = $(this).attr("id").split("_");
    //access array of id
   $(".label_"+id[1]).addClass("red");
 }

then add the id/ classes onto your html:

<input id="CheckBoxList1_0" id="check_1" type="checkbox" class="check" name="CheckBoxList1$0" style="opacity: 0; "/>

<label for="CheckBoxList1_0" id="label_1">Mark Park</label>

1 Comment

sorry, the event for checkboxes is "change" not "click". then you'd probably need to detect whether it's checked or unchecked at that point.
0

Try something like this:

$("label[for='comedyclubs']").toggleClass("red");

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.