Jump to content

Changing Text Colour Based On Value


wright67uk

Recommended Posts

I'm trying to get the text, in my table, to change colour based on its value.

 

The code below doesn't seem to work, can anyone see where I'm going wrong or suggest a better idea?

 

<script type="text/javascript">
window.onload = function(){ format(); }


function format(){
var vals = document.getElementsByClassName('myValue')


for(x in vals){
val = parseInt(vals[x].innerHTML);
vals[x].style.color = (val<=20) ? '#0f0' : '#f00';
}
}
</script>
</head>


<body>
<tr><td class='myValue'>25</td></tr>
</body>

Link to comment
Share on other sites

I am guessing you need to have proper HTML table structure, essentially, add <table> and </table> around "<tr><td class="myValue">25</td></tr>"

 

<body>
<table>
 <tr>
   <td class='myValue'>25</td>
 </tr>
</table>
</body>

 

Doing this made the code work and changed the color of 25 to red, good luck!

Link to comment
Share on other sites

Jessica is right, but to do it in JS is easy. They may not have JS enabled though, so it wouldn't then be shown.

Demo: http://xaotique.no-ip.org/tmp/29/

 

HTML

<span class="myClass">10</span>
<span class="myClass">13</span>
<span class="myClass">45</span>
<span class="myClass">26</span>
<span class="myClass">18</span>
<span class="myClass">5</span>
<span class="myClass">142</span>
<span class="myClass">16</span>
<span class="myClass">96</span>

 

Javascript

window.addEventListener("load", function()
{
   var elements = window.document.querySelectorAll(".myClass");

   for (var i in elements)
   {
       elements[i].style.color = Number(elements[i].innerHTML) >= 20 ? "#00FF00" : "#FF0000";
   }
}, false);

Link to comment
Share on other sites

Hello, and thank you for the suggestions.

 

Thanks for the code example Xaotique

 

Here is my table;

 

http://www.treequoter.com/tabledisplay3.php

 

The only thing, is that when I select page 2 or decide to display 100 results, then only 25 of the entries have been formatted.

 

So how would I do this in php? I'm guessing some sort of if statement after the while loop, but I have never really done anything like this before.

Link to comment
Share on other sites

If you were to finish that JS code btw, you would just trigger it to color them when you do your change of data.

 

function colorize()
{
   var elements = window.document.querySelectorAll(".myClass");

   for (var i in elements)
   {
       elements[i].style.color = Number(elements[i].innerHTML) >= 20 ? "#00FF00" : "#FF0000";
   }
}

window.addEventListener("load", colorize, false);

 

Then call colorize() when you change the data.

Link to comment
Share on other sites

Thank you very much to the both of you.

 

I went for the if statement this time.

But its great to learn how to do it the js way as well.

 

while($row=mysql_fetch_array($result)) {
   $fname=$row["fname"];
   $lname=$row["lname"];
   $club=$row["club"];
   $sdate=$row["sdate"];
$yard=$row["yard"];
$year=$row["year"];

if ($yard < 100)
{ 
   echo "<tr><td>$fname</td><td>$lname</td><td>$club</td><td>$sdate</td><td><span class='red'>$yard</span></td><td>$year</td></tr>";
}
else
{
echo  "<tr><td>$fname</td><td>$lname</td><td>$club</td><td>$sdate</td><td><span class='green'>$yard</span></td><td>$year</td></tr>";
}}

Link to comment
Share on other sites

It's good to wrap your variables within a string inside {}. Also, a cleaner way to do it would be:

 

$color = $yard < 100 ? "red" : "green";
echo "<tr><td>{$fname}</td><td>{$lname}</td><td>{$club}</td><td>{$sdate}</td><td><span class='{$color}'>{$yard}</span></td><td>{$year}</td></tr>";

 

Cuts your lines in half. You could also do it by adding on to the string with " . ($yard < 100 ? "red" : "green") . " which would take it down to one line, but I just prefer to not keep making one huge line.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.