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 
 1011  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: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

NEWSLETTER

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