To select specific elements, the cool kids all use custom selectors (not very
well documented, but see
and )

Basically, extend jQuery.expr[':'] with a string that will be evaluated to
true if you want that item selected.
You can use the variables 'a' for the element being considered, 'm[3]' for
whatever was in parentheses in the selector (as in the '2' in 'nth(2)' ),
'i' for the index of the element being considered, and 'r' for the whole
array of elements.

In short:
  jQuery.extend(jQuery.expr[":"], {
    positive : "parseFloat($(a).text()) > 0",
    negative : "parseFloat($(a).text()) < 0",
does what you want.

Use it like:

$('td:negative').css('color', 'red');
$('td:positive').css('color', 'green');


rolfsf wrote:
> Is there a clever way in jQuery to find non-zero data in specific columns
> of a table, and assign them a class based on whether they are positive or
> negative? In plain English, I want to make positive numbers green (or the
> cell bg) and the negative numbers red, but only in certain columns.
> Ideally, I would want to fine tune the range - say, > 1.5 is green and <
> -1.5 is red. I suspect that COL and COLGROUP are not very useful as
> selectors.
> Thanks!

View this message in context:
Sent from the JQuery mailing list archive at

jQuery mailing list

Reply via email to