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
2010-09-09 14:41:45
+1
A:
refer : :last , $.eq() for information
$('td:last').addClass('addedClass');
or
var td = $('td');
td.eq(td.length - 1).addClass('addedClass');
Ninja Dude
2010-09-09 14:44:47
This gets the absolute last cell, now the last in each row.
Nick Craver
2010-09-09 14:45:41
@nick Here the OP mentioned `I want to add a class to the last cell in the row `
Ninja Dude
2010-09-09 14:50:07
@Avinash - Yes, exactly what I mean..."in the row" :) How is this per row?
Nick Craver
2010-09-09 14:51:58
@ Avinash and Nick -- what is wrong with what I have? And sorry guys, what I meant is "in each row".
hersh
2010-09-09 14:53:02
@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
2010-09-09 14:54:11
@nick you should comment on the OP's question :), 'cause your comment is not related to this answer lol
Ninja Dude
2010-09-09 15:01:03
@Avinash - It's related to the comment directly above it...
Nick Craver
2010-09-09 15:03:42