0

I was able to find a few examples and pieced things together to get this working.

$(".status").filter(function () {
    return (/\(\)/).test($(this).html());
})
    .closest(\'td\').css(\'background-color\', \'red\');
});

However, I need to take this further and evaluate the negative value for a scalable conditional format.

if value of td is positive backgrond-color should be green. if value of td is negative which is representied in parantheses (###,###) then I want to evaluate based on these conditions.

(1) thru (199,999) =  .closest('td').css('background-color', 'yellow');

(200,000) or greater = .closest('td').css('background-color', 'red');
2
  • Why are you escaping all the quotes? Commented Sep 25, 2013 at 19:40
  • the escaped quotes came out of an echo from PHP. Commented Sep 25, 2013 at 20:52

1 Answer 1

1

.css() accepts a function as the value parameter.

Here's one possible solution:

http://jsfiddle.net/JUJBj/

$(".status").filter(function () {
    return (/\(.+\)/).test($(this).html());
}).closest('td').css('background-color', function() {
    var val = parseInt($(this).find('.status').text().replace(/\(|\)/g, ''), 10);
    return val >= 200000 ? 'red' : 'yellow';
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm marking this as the accepted, but have a question. Your example works with the <span> included, why is it that when the <span> is removed and class assigned to the <td> it only assigns yellow?
It's because of the $(this).find('.status') part. this is the td, and after your change, doesn't have a descendant with the class status, so the whole thing fails. Yellow is just the default. Here's an updated fiddle: jsfiddle.net/ttaA2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.