First time poster.
I am trying to change the background colour after clicking a button. I have tried a few different code types and what seems to be consistent is that the array doesn't get iterated properly.
Please Help
var color = ["red", "green", "blue"];
var i = 0;
document.querySelector("button").addEventListener("click", function() {
i = i < color.length ? ++i : 0;
document.querySelector("body").style.background = color[i];
})
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
button {
position: absolute;
top: 50%;
left: 50%;
cursor: pointer;
}
body {
background-color: blue;
}
<button>Click me to change background colour.</button>