<script>
$(document).ready(function() {
var refreshId = setInterval(function() {
if ($('#recent td') > '200') {.css("background-color", "#1eff00")};
},1000);
});
</script>

The above is what i'm trying to achieve but its definitely not working!
I have a table being outputted by PHP from a MySQL Db but i need to highlight a cell if the value is above 200 (or any value) is this possible with jQuery? #recent is the table id!

Thanks

Recommended Answers

All 6 Replies

Of course it is possible. You need to use .text() or .html() to get to the table cell value. Comparing a selector to a value won't work.

Cool,

With the my code i pasted in, where would i need to make the changes?

Thanks

<script type="text/javascript">
$(document).ready(function() {
var refreshId = setInterval(function() {

$("#recent tr").each(function() {
var tempresult = $("td").text();
});
if (tempresult < 200)
{
$("td").css("background-color", "red")
};

},1000);
});
</script>

Ok i read that link you gave, i've tried the code above but it's not working, does this look right to you?

$(document).ready(function() {
  $("#recent td").each(function () {
    if ($(this).text() > 100)
      $(this).css("background-color", "green");
  });
});

Thanks for that, i went with the below in the end, works great!

<script type="text/javascript">
$(document).ready(function() {
var refreshId = setInterval(function() {

         $("#recent tr:not(:first)").each(function() { 
             
             var tempresult = $(this).find("td:nth-child(3)").text();
             var setpoint = 200;
             
             if (tempresult < setpoint){
                 //change the color of the text to green if its a positive number
                 $(this).find("td:nth-child(3)").css("background-color", "#00FF00");
             }
             else if(tempresult > setpoint){
                 //change the color of the text to red if its a negatice number
                 $(this).find("td:nth-child(3)").css("background-color", "#FF0000");
				 $(this).find("td:nth-child(3)").css("color", "white");
             }
         });

},1000);
});
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.