jquery selecting all elements except the last per group

Posted by Anthony on Stack Overflow See other posts from Stack Overflow or by Anthony
Published on 2010-03-08T12:07:43Z Indexed on 2010/03/08 12:21 UTC
Read the original article Hit count: 256

Filed under:
|

I have a table that looks like:

<table>
   <tr>
      <td>one</td><td>two</td><td>three</td><td>last</td>
   </tr>
   <tr>
      <td>blue</td><td>red</td><td>green</td><td>last</td>
   </tr>
   <tr>
      <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>last</td>
   </tr>
</table>

What I want is a jquery selector that will choose all but the last td of each table row. I tried:

$("tr td:not(:last)").css("background-color","red");
  //changing color just as a test...

But instead of all cells but the last on each row being changed, all cells but the very last one in the table are selected. Similarly, if I change it to:

$("tr td:last").css("background-color","red");

the only one that changes is the very last cell. How do I choose the last (or not last) of each row?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors