Paul Attuck Paul Attuck - 3 months ago 15
CSS Question

:last for table

<table>

<tr><td>First Row</td><td>2 Row</td><td>1 last</td></tr>
<tr><td>Middle Row</td><td>2 Row</td><td>2 last</td></tr>
<tr><td>Last Row</td><td>2 Row</td><td>3 last</td></tr>

</table>


$('tr').each(function(index) {
$("td:last").css({backgroundColor: 'yellow', fontWeight: 'bolder'});
});


LIVE: http://jsfiddle.net/DxVG5/1/

why this working only for 3 last, but on 1 last and 2 last no? how can i fix it?

Answer

You need to add the context of the td, you're already in a for loop so the 'this' variable can be used. In this context 'this' is equal to the tr you're currently iterating on.

$('tr').each(function(index) {
    $("td:last",this).css({backgroundColor: 'yellow', fontWeight: 'bolder'});
});
Comments