Random Color Generator

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>generate a random color</h1>
<button>Click me</button>
<div>This is your new color</div>
  <script src="index.js"></script>
</body>

</html>

body{
    text-align: center;
}

div{
    height: 100px;
    width: 1000px;
    border: 1px solid black;
    margin: auto;
}




let btn = document.querySelector("button");

btn.addEventListener("click", function(){
    let h3 = document.querySelector("h1");
    let randomColor = getRandomColor();
    h3.innerText = randomColor;

    let div = document.querySelector("div")
    div.style.backgroundColor = randomColor;

    console.log("color updated");
})

function getRandomColor(){
    let red = Math.floor(Math.random()*255)
    let Green = Math.floor(Math.random()*255)
    let Blue = Math.floor(Math.random()*255)
    let color = `rgb(${red}, ${Green}, ${Blue})`;
    return color
}




Comments

Popular posts from this blog

form event second

dom basic practice

string practice test