Search code examples
phpjqueryhtml-tablebackground-colortablesorter

Can I change the <td> background if a field result is great or equal to another field?


I am displaying mySQL in an html table.

I would like to change the TD background color of $qty to red IF $qty >= $max or $qty =< $min.

Is there a simple a way to do this with jQuery or PHP?

I simplified my table and PHP for my example:

<table id="tablesorter-demo">
<tr><td>'.$min.'</td><td>'.$max.'</td><td>'.$qty.'</td></tr>
</table>

Solution

  • var min = $('table tr td:eq(0)').text();
    var max = $('table tr td:eq(1)').text();
    var qty = $('table tr td:eq(2)').text();
    
    if (qty >= max || qty <= min ) {
       $('table tr td:eq(2)').css('background-color', 'red');
    }
    

    http://jsfiddle.net/7vUFS/3/