views:

17

answers:

1

I have the following check box list inside table. I want to hilight the <td> when a checkbox is checked and unhilight when checkbox is unchecked. I know that I need to add class to <td> when checkbox is checked and remove class when unchecked.

<table id="cbDepartment">
    <tbody><tr>
        <td><input type="checkbox"  name="cbDepartment0" id="cbDepartment_0"><label for="cbDepartment_0">Check Box 1</label></td>
        <td><input type="checkbox"  name="cbDepartment1" id="cbDepartment_1"><label for="cbDepartment_1">Check Box 2</label></td>
        <td><input type="checkbox"  name="cbDepartment2" id="cbDepartment_2"><label for="cbDepartment_2">Check Box 3</label></td>
    </tr><tr>
        <td><input type="checkbox"  name="cbDepartment3" id="cbDepartment_3"><label for="cbDepartment_3">Check Box 4</label></td>
        <td><input type="checkbox"  name="cbDepartment4" id="cbDepartment_4"><label for="cbDepartment_4">Check Box 5</label></td>
        <td><input type="checkbox"  name="cbDepartment5" id="cbDepartment_5"><label for="cbDepartment_5">Check Box 6</label></td>
    </tr><tr>
        <td><input type="checkbox"  name="cbDepartment6" id="cbDepartment_6"><label for="cbDepartment_6">Check Box 7</label></td>
        <td><input type="checkbox"  name="cbDepartment7" id="cbDepartment_7"><label for="cbDepartment_7">Check Box 8</label></td>
        <td><input type="checkbox"  name="cbDepartment8" id="cbDepartment_8"><label for="cbDepartment_8">Check Box 9</label></td>
    </tr>
</tbody>
</table>
+1  A: 

Here is one way to do it:

jQuery(function() {
  $('#cbDepartment :checkbox').click(function() {
    var cell = $(this).closest('td');

    if ($(this).is(':checked')) {
      cell.addClass('check');
    }
    else {
      cell.removeClass('check');
    }
  });
});

Live example: http://www.jsfiddle.net/VTSXz/2/

htanata
Thank for the this one.
Narazana