When I attempt to dynamically change the color of text via on click I seem to have no problem using style.color="___" however, this doesn't seem to want to work for the background color of divs. Is there a particular reason for this?
var text = document.getElementById("text");
var square = document.getElementById("square");
text.onclick = function() {
if (this.style.color === "blue") {
this.style.color = "black";
console.log("it worked");
} else {
this.style.color = "blue";
}
};
square.onclick = function() {
if (this.style.backgroundColor === "#ccc") {
this.style.backgroundColor = "#fff";
} else {
this.style.backgroundColor = "#ccc";
}
};
#text {
color: blue;
}
#square {
height: 50px;
width: 50px;
background-color: #ccc;
transition: all .5s ease;
}
<p id="text">Test</p>
<div id="square"></div>