Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbers & colors

Status
Not open for further replies.

jacobus

Programmer
Apr 16, 2001
15
NL
Hi,

is it possible for positive numbers to be green and negative numbers be red using CSS?

thanks
jacobus
 
Jacobus,

What exactly are you trying to achieve? Where are the negative and positive numbers coming from?

Graham
 
Hi jacobus,

Here are 2 examples that works in IE and NN6. For both examples it's necessary that all the numbers are enclosed with HTML-tags :


1st example, if the number could be in any HTML tag:
<HTML>
<HEAD>
<script language=&quot;javascript&quot;>
[ignore]
function changeColor()
{
var di = document.body.all;

for (i=0; i<di.length; i++)
{
if (di.innerHTML < 0)
{
di.style.color = &quot;red&quot;;
}
else if (di.innerHTML >= 0)
{
di.style.color = &quot;green&quot;;
}
}
}
</script>
[/ignore]
</HEAD>
<BODY onload=&quot;changeColor()&quot;>
<br>
<table border=1>
<tr>
<td>1
</td>
<td>-1
</td>
<td>3
</td>
</tr>
<tr>
<td>0
</td>
<td>-4
</td>
<td>6
</td>
</tr>
<tr>
<td>-12
</td>
<td>text
</td>
<td>9
</td>
</tr>
</table>
<br>
<p>This is a text with a positive number <span>25</span> and a negative number <span>-34.56</span> and a zero <span>0</span></P>
<br>
<div>3000</div>
<div>-3000</div>
<span>-23 pages</span> (The text &quot;23 pages&quot; stays black!)<br>
<span>-23</span> pages
<div>-3</div>
<div>-0.0001</div>
<br>
</BODY>
</HTML>

This example loops trough ALL the HTML-tags, so also the <br>, <table>, <tr>, <p> etc. who are containing other text or nested HTML-tags. If you have a large file the performance could got worse.

2nd example, if the number only are in a specific HTML tag:
[ignore]
<script language=&quot;javascript&quot;>
function changeColor()
{
for (i=0; i<di.tags('TD').length; i++)
{
if (di.tags('TD').innerHTML < 0)
{
di.tags('TD').style.color = &quot;yellow&quot;
}
else if (di.tags('TD').innerHTML >= 0)
{
di.tags('TD').style.color = &quot;orange&quot;
}
}

for (i=0; i<di.tags('DIV').length; i++)
{
if (di.tags('DIV').innerHTML < 0)
{
di.tags('DIV').style.color = &quot;blue&quot;
}
else if (di.tags('DIV').innerHTML >= 0)
{
di.tags('DIV').style.color = &quot;#FF40FF&quot;
}
}

for (i=0; i<di.tags('SPAN').length; i++)
{
if (di.tags('SPAN').innerHTML < 0)
{
di.tags('SPAN').style.color = &quot;red&quot;
}
else if (di.tags('SPAN').innerHTML >= 0)
{
di.tags('SPAN').style.color = &quot;green&quot;
}
}
}
</script>
[/ignore]

Hope this helps,
Erik
<!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
Hi Erik & Graham,

I'm using a Tabular Data Control ActiveX to read out a textfile and displaying the numbers in a table.

Erik, I tried both examples and they work fine.
Thanks mate.

Good luck in Germany.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top