Page 1 of 1

Conditional Formatting

Posted: Mon 03 Dec 2012 7:05 am
by Alan
Hello there,
I am hoping that somebody will be able fix me up with a little bit of code (I am not even sure if what I am wanting to do is possible). I would like to be able to change the text colour of the <#temp> and <#tempunit> outputs from their current green to say red when the value of the <#temp> tag reaches 38°C. For the want of a better term I would think that this would be considered 'conditional formatting'.


<td width="44%"align="left"valign="center"style="font-size:1400%"style="color:#228B22"><#temp><#tempunit></td>



http://home.exetel.com.au/retnuh/weather/lite.htm

I would imagine that if it can be done the code would be inserted in the line just prior to (style="color:#228B22").
Any help is greatly appreciated.
Alan

Re: Conditional Formatting

Posted: Mon 03 Dec 2012 8:56 am
by sfws
(deleted due to errors in suggested code)

Re: Conditional Formatting

Posted: Mon 03 Dec 2012 10:40 am
by Alan
Thank you sfws, I inserted your suggested code but unfortunately the text is now black whilst <#temp> is under 38° instead of the usual green. I have experimented with your code and changed the 38° to 15° which is way below current temp to see if it would change to red but no, no go, still black.
At a loss, cheers!

Re: Conditional Formatting

Posted: Mon 03 Dec 2012 11:44 am
by beteljuice
Small errors in swfs code (been there, done that :lol: )
The colour red is attributed for both sides of the argument, and in splitting the code the quotes syntax is incorrect.

Try:

Code: Select all

<script type="text/javascript">
var code_segment;
if(<#temp> >=38){
   code_segment='<span style="color: red"><#temp><#tempunit></span>';
}
if(<#temp> <=0){
   code_segment='<span style="color: blue"><#temp><#tempunit></span>';
}

var panel=document.getElementById("panel");
panel.innerHTML=code_segment;
</script>
(Untested) I've made the code so it will a bit easier for you to edit, and given you a 'cold' option.

However; you do have more than one temperature on the 'page', and as written this will only work for one (the id 'panel'). It would be better written as function call if you want more than one coloured temperature.

Re: Conditional Formatting

Posted: Mon 03 Dec 2012 1:10 pm
by Alan
Great, thank you very much. Picked up the gist and applied small mods and additions so as to comply with my required temperature range and switching points. Works beautifully!!
Thanks and cheers!!