Advertisement
BenTibnam

Four Square Random Color

Nov 11th, 2020 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.18 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Untitled Document</title>
  6. <style>
  7.     #color-1{
  8.         width: 100px;
  9.         height: 100px;
  10.         position:absolute;
  11.         border-style: solid;
  12.         border-width: 2px;
  13.         border-color: lightslategray;
  14.     }
  15.    
  16.     #color-2{
  17.         width: 100px;
  18.         height: 100px;
  19.         position: absolute;
  20.         left:110px;
  21.         border-style: solid;
  22.         border-width: 2px;
  23.         border-color: lightslategray;
  24.     }
  25.    
  26.     #color-3{
  27.         width: 100px;
  28.         height: 100px;
  29.         position: absolute;
  30.         top: 110px;
  31.         border-style: solid;
  32.         border-width: 2px;
  33.         border-color: lightslategray;
  34.  
  35.     }
  36.    
  37.     #color-4{
  38.         width: 100px;
  39.         height: 100px;
  40.         position: absolute;
  41.         top: 110px;
  42.         left: 110px;
  43.         border-style: solid;
  44.         border-width: 2px;
  45.         border-color: lightslategray;
  46.     }
  47.    
  48.     #rand-btn{
  49.         position: absolute;
  50.         left: 50px;
  51.         top: 220px;
  52.     }
  53.    
  54.     body{
  55.         position: absolute;
  56.         left: 43%;
  57.     }
  58.    
  59.     #color-text{
  60.         position: absolute;
  61.         top: 300px;
  62.         font-size: 48px;
  63.         font-style:italic;
  64.         font-weight: bolder;
  65.     }
  66. </style>
  67. </head>
  68.  
  69. <body>
  70.     <div id = "color-1"></div>
  71.     <div id = "color-2"></div>
  72.     <div id = "color-3"></div>
  73.     <div id = "color-4"></div>
  74.     <div id = "color-text"> </div>
  75.    
  76.     <input type = "button" value = "Randomize Colors" id = "rand-btn" onclick="changeFourColors()"/>
  77.    
  78.     <script>
  79.         var colorOneArea = document.getElementById("color-1");
  80.         var colorTwoArea = document.getElementById("color-2");
  81.         var colorThreeArea = document.getElementById("color-3");
  82.         var colorFourArea = document.getElementById("color-4");
  83.         var colorTextArea = document.getElementById("color-text");
  84.        
  85.         randColors = function(divToChange){
  86.             var colorValues = "0123456789ABCDEF";
  87.             var colorCode = "#";
  88.             for(var i = 0; i < 6; i++){
  89.                 colorCode += colorValues.charAt(Math.round(Math.random() * 15));
  90.             }
  91.            
  92.             colorTextArea.innerHTML += colorCode + "<br/>";
  93.            
  94.             divToChange.style.backgroundColor = colorCode; 
  95.         }
  96.        
  97.         changeFourColors = function(){
  98.             colorTextArea.innerHTML = "";
  99.             randColors(colorOneArea);
  100.             randColors(colorTwoArea);
  101.             randColors(colorThreeArea);
  102.             randColors(colorFourArea);
  103.         }
  104.        
  105.         changeFourColors();
  106.                
  107.     </script>
  108. </body>
  109. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement