HOW CAN I REFERENCE A HTML TABLE CELL TD FROM A CHECKBOX INSIDE THE CELL USING JQUERY?

Jeet Verma

Total Post:110

Points:776
Posted by  Jeet Verma
JQuery 
 859  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. marcel ethan

    Post:105

    Points:735
    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":"");

    });

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!