How can I reference a html table cell td from a checkbox inside the cell using jquery?

Total Post:110

Points:776
JQuery 
 1198  View(s)
Ratings:
Rate this:

I have an html table:

  <table>
         <tr>
              <td>
                  <input type="checkbox" name="myCheck">
              </td>
         </tr>
  </table>

I want to change the backcolor of the table cell when i click on the checkbox. The table cell doesn't seem to be the parent of the checkbox.

What is the correct way to get a reference to the table cell on checkbox click?

 

  1. Post:104

    Points:728
    Re: How can I reference a html table cell td from a checkbox inside the cell using jquery?

    You can use currently clicked checkbox context this along with .parent() in change event to target the td element:

    $('input[name="myCheck"]').change(function(){
      $(this).parent().css('background',this.checked?"red":"");
    });

      Modified On Apr-08-2018 11:36:15 PM

Answer