JS Change Color

by anoopsuda

HTML

<div>
<button onclick="changeColors()">
Change color
</button>
</div>

CSS

div {width:100%; height:100vh;}

JavaScript

var index = 0;

function changeColors()
{
var colors = ["red", "blue","orange", "black"];
//alert(colors[index++]);
document.getElementsByTagName("div")[0].style.background = colors[index++];
if(index > colors.length - 1)
index = 0;
}