Knowledge Walls
John Peter
Pune, Maharashtra, India
How to generate random colors in javascript with Example
3062 Views
RandomColors
<html>
    <head>
        <style type="text/css">
            #magic_div {
               padding: 20px;
               display: inline-block;
               background-color: yellow;
            }
        </style>
        <script type="text/javascript">
            function colorIt(){
                var colorName = "rgb("+(Math.random()*255).toFixed()+","+(Math.random()*255).toFixed()+","+(Math.random()*255).toFixed()+")";
                document.getElementById('magic_div').style.backgroundColor = colorName;
            }
            window.onload = function(){
               setInterval("colorIt()",400);
            };
        </script>
    </head>
    <body>
        <div id="magic_div"></div>
    </body>
</html>
Demo & Output 
Next
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details