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

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

This is what I do:

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

But this code returns every td, not just the last of each row.

Can someone tell me how to fix this?

Thanks in advance.

0
source share
1 answer

You can just write

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

Your code is incorrect for several reasons:

  • .last() selects the last item in the current set; you need .children().last()
  • .each() does nothing with the return value of its callback; you want .map()
  • children() makes your callback for each cell in each row.
+8
source

All Articles