Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-07-2012, 03:09 PM   PM User | #1
alvaritoe
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
alvaritoe is an unknown quantity at this point
writing different font depending on comparision result inner.html value

Hello

I have got this result

Code:
document.getElementById('rent').innerHTML
which outputs a number alright. However, I d like to be a bit fancier and want to output it red if it is equal to 5 or smaller and green otherwise.

I know that you change color by document.write(fontcolor("red"))

but I dont know how to embed this last document.write etc into the one I have above
alvaritoe is offline   Reply With Quote
Old 10-07-2012, 03:37 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Code:
<div id="rent"><div>
<script>
var x=3;
document.getElementById('rent').style.color=(x>5)?"green":"red";
document.getElementById('rent').innerHTML=x;
</script>
xelawho is offline   Reply With Quote
Old 10-07-2012, 04:01 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by alvaritoe View Post
I know that you change color by document.write(fontcolor("red"))
document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
fontcolor, innerhtml

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:19 AM.


Advertisement
Log in to turn off these ads.