Javascript Math Random Color via rgb()

Description

Javascript Math Random Color via rgb()

View in separate window

<!DOCTYPE html>
<head>
<style>
#num/*from  w w w. j a  v a  2 s  . c  om*/
{
   height: 100px;
   width: 100px;
   background-color: #000000;
}
</style>
<script>

window.onload=function() {
  document.onclick=ranNumber;
}

function randomVal(val) {
  return Math.floor(Math.random() * val);
}

function randomColor() {
   return "rgb(" + randomVal(255) + "," + randomVal(255) + "," + randomVal(255) + ")";
}

function ranNumber() {
   let rgb = randomColor();
   document.getElementById("num").setAttribute("style","background-color: " + rgb);
}
</script>

</head>
<body>
<div id="num"></div>
</body>
</html>



PreviousNext

Related