Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,912
    Thanks
    56
    Thanked 545 Times in 542 Posts
    Code:
    <div id="rent"><div>
    <script>
    var x=3;
    document.getElementById('rent').style.color=(x>5)?"green":"red";
    document.getElementById('rent').innerHTML=x;
    </script>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •