ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

jQuery Refresh the page to change the background-colour

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){ 
   
   var colors = ["khaki", "tomato", "seagreen", "cyan", "blue" ];
   
   var rand = Math.floor(Math.random() * colors.length);
   
   $('.box1 ').css("background-color", colors[rand]);
   
   $(".box1 #preseted").html(colors[rand]);
   
   var randomcolor = Math.floor(Math.random() * 16777215).toString(16);

   $('.box2').css('background-color', "#" + randomcolor); 

   $(".box2#preseted2").html("#" + randomcolor); 
});
</script>
</head>
<body> 
<p>By refreshing the page the background-color changes.</p> 
<p>Choses from these colors <b>khaki</b>, <b>tomato</b>, <b>seagreen</b>, <b>cyan</b> and <b>blue</b> will be select. </p>
<div class="box1" style="padding:3mm;">css color name: <h1 id="preseted"></h1></div> 
<p>Randomly choses a color. </p>
<div class="box2" style="padding:3mm;">css color name: <h1 id="preseted2"></h1></div>
</body>
</html>
Categroy:
Front-end , JQuery