0

I am trying to generate the random color code and also I want to display that color code into SPAN. Tried this below code,

<body>
  <script type="text/javascript">
        var randomColor = Math.floor(Math.random() * 16777215).toString(16);
        randomColor = "#" + ("000000" + randomColor).slice(-6);
        document.bgColor = randomColor;
        document.getElementById("hexRandomColor").innerHTML = randomColor;
  </script>

  <div class="container">
      <span id="hexRandomColor"></span>
  <div>
</body>

But, it (document.getElementById("hexRandomColor").innerHTML = randomColor;) didn't show the color code in span. It works, When I move the script code outside of body. But, I want to keep script code inside the body element. How do I display the color code?

Karuppiah RK
  • 3,894
  • 9
  • 40
  • 80

4 Answers4

1

Your script is executing before DOM is fully loaded which causes the element unavailable when trying to set the style and throws error:

Uncaught TypeError: Cannot set property 'innerHTML' of null

To solve the issue, you can either place the script at the bottom of the body or wrap your code with DOMContentLoaded which will ensure that the code inside will be executed only after the DOM is fully loaded.

<body>
  <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function(event) {
       var randomColor = Math.floor(Math.random() * 16777215).toString(16);
       randomColor = "#" + ("000000" + randomColor).slice(-6);
       document.bgColor = randomColor;
       document.getElementById("hexRandomColor").innerHTML = randomColor;
    });        
  </script>

  <div class="container">
      <span id="hexRandomColor"></span>
  </div>
</body>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

Use window.onload. Because when the script is executed the element doesnot exist

<body>
  <script type="text/javascript">
  window.onload = function(){
            var randomColor = Math.floor(Math.random() * 16777215).toString(16);
            randomColor = "#" + ("000000" + randomColor).slice(-6);
            document.bgColor = randomColor;
            document.getElementById("hexRandomColor").innerHTML = randomColor;
        }
  </script>

  <div class="container">
      <span id="hexRandomColor"></span>
  <div>
</body>
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

Put the script near the closing tag of body(</body>). The script is running before span is rendered in the DOM.Span is not in the DOM yet Hence you are getting cannot set innerHTML of null error.

<body>

 <div class="container">
      <span id="hexRandomColor"></span>
  </div>
  <script>
var randomColor = Math.floor(Math.random() * 16777215).toString(16);
        randomColor = "#" + ("000000" + randomColor).slice(-6);
        document.bgColor = randomColor;
        document.getElementById("hexRandomColor").innerHTML = randomColor;
        </script>
</body>
ellipsis
  • 12,049
  • 2
  • 17
  • 33
1

The HTML must be rendered first so the <script> should be at end tag </body>

<body>


  <div class="container">
      <span id="hexRandomColor"></span>
  </div>
  
    <script type="text/javascript">
        var randomColor = Math.floor(Math.random() * 16777215).toString(16);
        randomColor = "#" + ("000000" + randomColor).slice(-6);
        document.bgColor = randomColor;
        document.getElementById("hexRandomColor").innerHTML = randomColor;
  </script>
</body>
zer00ne
  • 41,936
  • 6
  • 41
  • 68