Home > DeveloperSection > Forums > How can I reference a html table cell td from a checkbox inside the cell using jquery?
Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    January-20-2015 10:02 PM

 JavaScript JQuery 
Ratings:


 1 Reply(s)
 488  View(s)
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?

 



marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    January-20-2015 10:37 PM

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

});


Don't want to miss updates? Please click the below button!

Follow MindStick