There are three issues with this code.
1) Your comparison in the if statement,
if (x.style.color="#000000")
, is not a comparison but an assignment, you're missing one = to make it a comparison. What basically happens is that the color property would get a value assigned, and then the result of that assignment (the value itself) is tested in the if statement, so it's always true.
2) After setting
x.style.color
to
#000000
, the color is not
#000000
but rather
rgb(0, 0, 0)
(even the spaces are important here)
3) Initially the color is not set, so x.style.color doesn't return anything. It did turn red, because of the flawed if statement.
So to make the code work, you'll get something like this:
function myFunction()
{
x=document.getElementById("demo")
if (!x.style.color || x.style.color=='rgb(0, 0, 0)')
{
x.style.color="#FF0000";
}
else
{
x.style.color="#000000";
}
}