Alex Guerin
Alex Guerin

Reputation: 2386

jQuery: Get last row column selector

I have the following row in a table:

<tr><td>Adric cut-out</td><td><span class="quantity">0</span></td><td>worthless</td></tr>

I've already got the span with the class 'quantity' selected, but I'm having no luck getting the last TD element in the row selected. I can do it by getting its parent, then its parent, then the last TD child element but is there something a little more efficient?

Using this as a start, how would I select the TD element containing the price? Note, it will always be the last item on the row.

Upvotes: 3

Views: 7680

Answers (3)

Marc
Marc

Reputation: 11613

See here: http://jsbin.com/odacar

Which alerts the last TD using this:

function doAlert() {
  alert($('td:last').html());
}

Upvotes: 2

rjz
rjz

Reputation: 16510

If the format is consistent, stepping up and over to the next column should be fairly efficient:

var price = $('.quantity').closest('td').next().html();

It might be faster to simply assign a class (price?) to the last td and collect that, though.

Upvotes: 3

westo
westo

Reputation: 575

You could use the last selector - http://api.jquery.com/last-selector/

$('tr').children('td:last')

Upvotes: 0

Related Questions