function changeColor() {
if (confirm("Press a button!") == true) {
if(this.style.backgroundColor == "white")
this.style.backgroundColor = "yellow";
else if(this.style.backgroundColor == "yellow")
this.style.backgroundColor = "red";
else if(this.style.backgroundColor == "red")
this.style.backgroundColor = ""
else
this.style.backgroundColor = "";
} else {
txt = "You pressed Cancel!";
}
if (confirm('Are you sure you want to save this thing into the database?') == true) {
// Save it!
} else {
// Do nothing!
}
}
var boxes = document.getElementsByClassName("foo");
for (i = 0; i < boxes.length; i++) {
boxes[i].addEventListener("click", changeColor);
}
.foo {
float: left;
width: 30px;
height: 30px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
border-radius: 25%;
}
.white{
background: #FFFFFF;
}
.whole {
float: left;
width: 900px;
height: 900px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
}
<div class="whole">
<div id="centerbox1" class="foo white">A1</div>
<div id="centerbox2" class="foo white">A2</div>
<div id="centerbox3" class="foo white">A3</div><br><br>
<div id="centerbox4" class="foo white">B1</div>
<div id="centerbox5" class="foo white">B2</div>
<div id="centerbox6" class="foo white">B3</div>
</div>
There seems to be an issue with the prompts and conditions not changing the color of the boxes as expected. The code was designed to change the colors when the user confirms a prompt, but for some reason, the behavior is not working as intended. Any insights on why this might be happening would be greatly appreciated.
For example, when clicking on a square, it should prompt a yes or no response, resulting in the color changing to yellow. Clicking on the same square again, which is now yellow, should prompt another confirmation and change the color back to the original color.
This question serves as a follow-up to the query found at div onclick prompts yes or no, if yes, div change to different color