views:

184

answers:

1

Hi,

I need some help with this

I am trying to do this

if(perc>0){
alert('change backgroundcolor and textcolor');
$('#stocktable tr td:last').addClass('stockhigher');

}

but It does not work on a tablecell

I also try'd to set the selector like this

$('#stocktable tr td:eq(2)).addClass...
$('#stocktable tr td.percentage').addClass...

nothing!

it does work on the table itself or a tablerow like

$('#stocktable tr')

am I missing something here?

thanks, Richard

+1  A: 

Three things spring to mind:

  1. You're using the :last pseudo-element. That will match at most one element total, in this case the very last table cell in "stocktable". Do you perhaps mean :last-child instead?
  2. You're using :eq(2) which will match the third element in the entire set only. Do you perhaps mean :nth-child(2)?
  3. $("#stocktable tr td.eq(2)).addClass... is missing and end quote; and
  4. There is nothing wrong with what you're doing. What precisely isn't working? Perhaps it's not formatting that can be applied to a table cell.

To further explain (1) imagine you have a table with 3 rows of 4 cells with an id of "mytable". This code:

$("#mytable td:eq(2)").css("background", "yellow");

will colour the third element of the first row (:eq() is zero-based) whereas:

$("#mytable td:nth-child(2)").css("background", "yellow");

will colour the second cell in each row.

$("#mytable td:last").css("background", "yellow");

will colour the very last cell in the very last row but:

$("#mytable td:last-child").css("background", "yellow");

will colour the last cell in each row.

cletus
thank you, now I wished you wherent so fast.I was being stupid, the problem was that I replaced all the td'stwo lines after the evaluation, sorry
Richard
if it's possible to delete the post then please do.
Richard