I seriously feel like a genuine idiot posting this, but I'm in need of help. I'm just learning Javascript and I'm messing around to experiment and something dead simple that should work just won't.
I have a dummy HTML file with a text headline with the id "mainHeading." I want to change the color of that text with a click -- the first click will turn it blue, the second red. The first click works fine and the text goes from black to blue. But the second click just leaves it blue.
Here's my Javascript:
var headline = document.getElementById("mainHeading");
headline.onclick = function() {
if (headline.style.color = "black") {
headline.style.color = "blue";
} else {
headline.style.color = "red";
}
alert("The color is " + headline.style.color);
};
Not sure why it's not working. I put that alert() in there so that I could be sure the color names are right. Sure enough, after the first click, headline.style.color is blue. Which means on the second click, it should skip to the else block and go red...right? But it isn't.
Again: I'm sure I'm a complete idiot, but... Anyone want to explain to me why I'm stupid?
I have a dummy HTML file with a text headline with the id "mainHeading." I want to change the color of that text with a click -- the first click will turn it blue, the second red. The first click works fine and the text goes from black to blue. But the second click just leaves it blue.
Here's my Javascript:
var headline = document.getElementById("mainHeading");
headline.onclick = function() {
if (headline.style.color = "black") {
headline.style.color = "blue";
} else {
headline.style.color = "red";
}
alert("The color is " + headline.style.color);
};
Again: I'm sure I'm a complete idiot, but... Anyone want to explain to me why I'm stupid?