user1295445 user1295445 - 3 months ago 37
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>

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

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


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?