tags:

views:

22

answers:

2

I have a grid which has x rows and 6 columns. I want to add a class to the last column in each row.

+1  A: 

To get the 5th cell, if it's the last, use :last-child, like this:

$(function() {
  $("#myTable td:last-child").addClass("myClass");
});

To get the 5th no matter what, use the :nth-child() selector, like this:

$(function() {
  $("#myTable td:nth-child(5)").addClass("myClass");
});
Nick Craver
+1  A: 

refer : :last , $.eq() for information

$('td:last').addClass('addedClass');

or

var td = $('td');
td.eq(td.length - 1).addClass('addedClass');
Ninja Dude
This gets the absolute last cell, now the last in each row.
Nick Craver
@nick Here the OP mentioned `I want to add a class to the last cell in the row `
Ninja Dude
@Avinash - Yes, exactly what I mean..."in the row" :) How is this per row?
Nick Craver
@ Avinash and Nick -- what is wrong with what I have? And sorry guys, what I meant is "in each row".
hersh
@hersh - It's adding a class to the `<tr>`, which is what `this` is....what do you want it do to, add it to the `<div>`? Also you're appending a `<div>` to a `<tr>`, which isn't valid...
Nick Craver
@nick you should comment on the OP's question :), 'cause your comment is not related to this answer lol
Ninja Dude
@Avinash - It's related to the comment directly above it...
Nick Craver