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?