Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select every visible last child in jQuery

I would like to get the last visible td in each tr in a table.

This does not work because it attempts to select the last child if it is visible:

var last_visible_cells = $(table).find("tr td:visible:last-child");

So far the simplest method I've thought of is to use a .each to loop through the tr elements and append each of the last visible tds to a new selector list.

Is there a simpler way? Does anything like this exist?

var last_visible_cells = $(table).find("tr").lastMatching("td:visible");
like image 388
Trey Hunner Avatar asked Dec 13 '11 21:12

Trey Hunner


1 Answers

You can do it:

$('table tr').find('td:visible:last').addClass('last-visible');

See a full example (jQuery 1.2+ compatible)

like image 167
David Rodrigues Avatar answered Oct 14 '22 19:10

David Rodrigues