Piotr Stapp
Piotr Stapp

Reputation: 19828

How to modify to every cell in last column in table using Jquery

I'm trying to modify every cell in last column in html table. My first try is:

   $('#example td:last').each(function(elem) {
       //do something with elem
   });

But above code modify only last cell in last column (so one cell instead of all cells in column).

How should I change selector to much all td in last column?

Upvotes: 0

Views: 1383

Answers (3)

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33880

:last return a single element, the last element of the jQuery stack. It is exactly the same of doing :

var $td = $('#example td');
$td.eq($td.length - 1);

What you want is the CSS selector :last-child, which return the last child (huh).

$('#example td:last-child').each(...);

Upvotes: 0

Bogdan Iulian Bursuc
Bogdan Iulian Bursuc

Reputation: 2275

Try :last-child instead of :last.

Upvotes: 1

Felix
Felix

Reputation: 38112

You can do:

$('#example tr').each(function() {
    var elem = $(this).find('td:last');
    //do something with elem
});

Fiddle Demo

Upvotes: 0

Related Questions