What I'm trying to do is that the div is painted red on the first click and the second click is aqua but does not work. For me the script is fine but I do not know what happens
var div;
var coloreado = 0;;
function clickdiv() {
div = document.getElementsByClassName("divcolor");
div[0].addEventListener("click", darcolor, false);
}
function darcolor() {
if (coloreado == 0) {
div[0].style.backgroundColor = "red";
coloreado = 1;
}
if (coloreado == 1) {
div[0].style.backgroundColor = "aqua";
coloreado = 0;
}
}
window.onload = clickdiv;
.divcolor {
height: 100px;
width: 100px;
background-color: aqua
}
<div class="divcolor">
</div>