-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.

2

1 Answer 1

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>`
Sign up to request clarification or add additional context in comments.

1 Comment

Its just an example. try to code and explore there are lots of examples provided in this site. for random color and to set property I too have taken help from them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.