Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector problem. :last-child not doing what I need

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

like image 309
jessegavin Avatar asked Jan 27 '10 21:01

jessegavin


1 Answers

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.

like image 165
Gumbo Avatar answered Oct 18 '22 12:10

Gumbo