How to insert colored text in html page using javascript

Discussion in 'JavaScript and AJAX' started by anchitjindal07, Mar 4, 2011.

  1. anchitjindal07

    anchitjindal07 New Member

    Joined:
    Jul 13, 2009
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    1
    Hi
    I am inserting text in html page using javascript..But the font-color of this text is black by default. How to change the color of this text??

    I am using createTextNode() and appendChild() functions to insert the text but not able to change the color using style.color property. How to accomplish this?
    Please help..
    Thanks a lot...
     
  2. Ami Desai

    Ami Desai Member

    Joined:
    Jan 5, 2017
    Messages:
    42
    Likes Received:
    17
    Trophy Points:
    8
    Location:
    Ahmedabad
    Home Page:
    http://www.ifourtechnolab.com/
    Hi,

    You can check this

    Code:
    <button onclick="myFunction()">Try it</button>
    
    <script>
    function myFunction() {
        var h = document.createElement("H1");
        h.style.color="red";
        var t = document.createTextNode("Hello World");
        h.appendChild(t);
        document.body.appendChild(h);
    }
    </script>
    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice