user1791841
user1791841

Reputation: 133

How to select the last td in every tr using .each() and .last()?

I want to select the last td in every tr using the .each() loop and .last() method.

This is what I'm doing:

$('tr:has(td)').children().each(function(){return $(this).parent().last()});

But that code returns every td and not just the last one from each row.

Can someone tell me how to fix it?

Thanks in advance.

Upvotes: 0

Views: 586

Answers (1)

SLaks
SLaks

Reputation: 887453

You can simply write

$('tr > td:last-child')

Your code is wrong for a number of reasons:

  • .last() selects the last element in the current set; you need .children().last()
  • .each() doesn't do anything with the return value of its callback; you want .map()
  • children() makes your callback run for every cell in each row.

Upvotes: 8

Related Questions