-1

This program is supposed to change the background color of the HTML body from a given array of colors. But the colors are not appearing as it should?

    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>I Can Click A Rainbow</title>
</head>

<body>
    <button id="button">click me</button>
    <script src="js/script.js"></script>
</body>

</html>

    var button = document.getElementById("button");

var rainbow = ["red", "orange", "green", "blue", "indigo", "violet"];

function change() {
    "use strict";

    document.body.style.background = rainbow[Math.floor(7 * Math.random())];
}

button.addEventListener("click change");
j08691
  • 204,283
  • 31
  • 260
  • 272
css expert
  • 13
  • 4

1 Answers1

2

Here is a working example

You have a typo here:

button.addEventListener("click change");

make sure to include your function as a function, not part of the string!

button.addEventListener("click", change);
Nick Zuber
  • 5,467
  • 3
  • 24
  • 48