Example Code to Change Background Color Randomly

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Random Background Color</title>
  <style>
    body {
      transition: background-color 0.5s ease;
    }
    button {
      padding: 10px 20px;
      font-size: 16px;
    }
  </style>
</head>
<body>

  <button onclick="changeColor()">Change Color</button>

  <script>
    function getRandomColor() {
      const letters = '0123456789ABCDEF';
      let color = '#';
      for (let i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
    }

    function changeColor() {
      document.body.style.backgroundColor = getRandomColor();
    }
     

Click to See a New Message

Welcome! Click the button to begin.

</script> </body> </html>