views:

144

answers:

1

What jQuery selector would allow me to select the last <td> in a <tr> that has more than 1 <td>? NOTE: I am trying to find an answer which will allow me to use a single selector or a selector followed by a .filter() rather than using .each()

<table>
  <tr>
    <td colspan="2">First Cell</td>
    <td>Second Cell</td>
  </tr>
  <tr>
    <td colspan="2">Only Cell</td>
  </tr>
  <tr>
    <td>First Cell</td>
    <td>Second Cell</td>
    <td>Third Cell</td>
  </tr>
</table>

At first, I thought td:last-child might work, but it doesn't.

alt text

+8  A: 

Try this selector:

td:last-child:not(:first-child)

The explanation: In you case the last child is also the first child as it’s the only child of the parent element. If you now select only the last children that are also not the first childen of their parent element, you’ll get only the last children that are not the only children of their parent element.

Gumbo
That didn't take long! Thank you it works.
jessegavin
+1. I'm going to study this and figure out why it works!
Patrick Karcher