I'm attempting to create color-changing "squares" that change color when clicked. The first square changes color correctly, but when I click on the others, the color change only happens on the first square.
var image_tracker = 'red';
function changeColor() {
var image = document.getElementById('red')
if (image_tracker == 'red') {
image.style.backgroundColor = "blue";
image_tracker = 'blue';
} else {
image.style.backgroundColor = "red";
image_tracker = 'red';
}
}
<div id="red" onclick="changeColor()"></div>
<div id="red" onclick="changeColor()"></div>
<div id="red" onclick="changeColor()"></div>
There are no error messages, it's just not working as I expected :D