Paul Attuck Paul Attuck - 1 year ago 108
CSS Question

:last for 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>


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


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

Answer Source

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'});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download