user1295445 user1295445 - 29 days ago 14
jQuery Question

How to find previous table cell using jquery?

I have a 4 col table, n rows.

<table border="0">
<tr><td><p>Cell 1 text</p> </td><td>chkbox in cell2</td><td><p>Cell 3 text</p></td><td>chkbox in cell4</td>
</tr>
</table>


I want to find and change css of previous cell on click of checkbox. So if checkbox in any row, cell 2 is clicked I need to act on contents of that row, cell 1, or if any row, cell 4 is clicked I need to act on that row, cell 3.
I have tried
$(this).parents('td:first').find('p').text();

but this gets both cell 1 & cell 3
I do not really want to assign an id to all the cells. Any suggestions please?

Answer

To directly answer your question in the title, you can use the following:

$('checkbox').change(function() {
   var prevCell = $(this).closest('td').prev();

   // do stuff with prevCell
})

As for your problem in the description... Can't really understand them, could you post a more elaborate description?

Comments