Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 555
Richard Avatar asked Apr 04 '12 09:04

Richard


1 Answers

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?

like image 157
Andreas Wong Avatar answered Oct 12 '22 00:10

Andreas Wong