Prashant Prashant - 2 months ago 9
jQuery Question

How to select a row in table using jquery?

I have created a table in my application, I want to select (change background color) whole row on click of a checkbox, same as gmail is doing, When we click checkbox in gmail, the whole row becomes yellow.

<table>
<tbody>
<tr>
<td><input type="checkbox" name="chk" id="chk" /></td>
<td>My Name</td>
<td>Description of the job</td>
</tr>
</tbody>
</table>


Please tell me how to do the same in jquery?

Answer
$(function() {
  $("#chk").click(function() {
    $(this).parents("tr").toggleClass("diffColor");
  });
});

Create a CSS class (called "diffColor" above) and add the background color that way, something like:

<style type="text/css">
tr.diffColor td { background-color: yellow; }
</style>

Don't set CSS attributes directly. Use classes where possible.

Comments