Colorize negative/positive numbers (jQuery)

colors, jquery, numbers, readability

Solution

Here ya go:

$(document).ready( function() {

  // the following will select all 'td' elements with class "of_number_to_be_evaluated"
  // if the TD element has a '-', it will assign a 'red' class, and do the same for green.

  $("td.of_number_to_be_evaluated:contains('-')").addClass('red');
  $("td.of_number_to_be_evaluated:contains('+')").addClass('green');
}

Then use CSS to style the input elements:

td.red {
  color: red;
}

td.green {
  color: green;
}

Problem

I'd like to color numbers in a table for better readability: - green for positive (+00.00); - red for negative (-00.00) and; - black for default case (no sign)

Original source