-1

I am not a Javascript expert and I need a little help. I am using 9 div classes in my html it show like rectangle box.

enter image description here

I want my result like this. when i refresh my page all the colors should be change randomly using javascript can any give the good result for me. and give me the code.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Vinoth
  • 74
  • 1
  • 11
  • post you code what you tryed – Sarath Aug 10 '16 at 13:14
  • 2
    Random color: http://stackoverflow.com/questions/1484506/random-color-generator-in-javascript Change div's background: http://stackoverflow.com/questions/1874560/how-to-use-javascript-to-change-div-backgroundcolor Try to code a bit, and if it doesn't work come again and post a question with your code. – yuriy636 Aug 10 '16 at 13:14

1 Answers1

0

Its a bit silly, hope you like it and it is that what you want.

<html>
    <head>
        <style>
            .divs{
                float:left; 
                width:30%; 
                height: 150px; 
                margin:5px;
            }
        </style>
    </head>
    <body onload="changeBackground();">
        <div>
            <div id="d1" class="divs"></div>
            <div id="d2" class="divs"></div>
            <div id="d3" class="divs"></div>
        </div>       
        <div>       
            <div id="d4" class="divs"></div>
            <div id="d5" class="divs"></div>
            <div id="d6" class="divs"></div>
        </div>       
        <div>        
            <div id="d7" class="divs"></div>
            <div id="d8" class="divs"></div>
            <div id="d9" class="divs"></div>
        </div>
        <script>
          function changeBackground() {
            var i=1;
            for(i=1;i<=9;i++){
            document.getElementById("d"+i).style.backgroundColor =
 '#'+Math.floor(Math.random()*16777215).toString(16); 
            }
        }
        </script>
    </body>
</html>`
Amrendra
  • 511
  • 8
  • 18
  • Its just an example. try to code and explore there are lots of examples provided in this site. [for random color](http://stackoverflow.com/questions/1484506/random-color-generator-in-javascript) and [to set property](http://stackoverflow.com/questions/1874560/how-to-use-javascript-to-change-div-backgroundcolor) I too have taken help from them. – Amrendra Aug 10 '16 at 14:38