thedeadlyone
thedeadlyone

Reputation: 59

How to add class to last tr of the table using jquery

The code have to work with ie so :last is not an option

<table class="ms-main" cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
<tr>..</tr>
<tr>..</tr>
<tr>..</tr>
<tr class=add this value including class=>..</tr>
</table>

Thank you in advance

EDIT1:

How about if the source file is this?

<table class="ms-main" cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
<tr>..</tr>
<tr>..</tr>
<tr>..</tr>
<tr class=add this value including class=>**<table>more nested tables here</table>**</tr>
</table>

Upvotes: 2

Views: 3540

Answers (3)

David Hedlund
David Hedlund

Reputation: 129782

:last does work in IE.

jQuery implements this manually, it has no relation to the css selectors supported by the browser.

$('.ms-main tr:last').addClass('name-of-class');

Here's a nice article on jQuery's implementation of the sizzle selector and use of querySelectorAll

As for your edit, you may want to use > to denote that the tr should be an immediate child, and not a more distant descendant.

$('.ms-main > tr:last')

Look out for tbody, though.

Upvotes: 1

Gopal
Gopal

Reputation: 217

<script>
   $("table tr:last").addClass("lastOne");
</script>

Upvotes: 0

Ozerich
Ozerich

Reputation: 2000

$('table tr').last().addClass('myClass');

Selector :last-child is not supported in IE 6-8, use method last() to find last element in DOM

Upvotes: 0

Related Questions