Here's my code:
if (document.getElementById("hiddenButton").style.visibility != "visible") {
document.getElementById("hiddenButton").style.visibility = "visible";
}
else {
document.getElementById("hiddenButton").style.visibility = "hidden";
}
This code shows and hide a HTML button when you click on another button.
But my question is why does that code work and this doesn't:
if (document.getElementById("hiddenButton").style.visibility = "hidden") {
document.getElementById("hiddenButton").style.visibility = "visible";
}
else {
document.getElementById("hiddenButton").style.visibility = "hidden";
}
Left = Right
This means, "Whatever the right side is, put it as the value for the left side."
All comparisons and other checks are done with two symbols to limit ambiguity and improper variable assignments when you simply meant to check a value.
Your condition is actually an assignment:
You should be using
==
:I think your problem is that you are confusing the assignment operator ( = ) with the equality operator ( == or ===). the assignment operator set the left hand side equal to whatever is on the right hand side, and the equality operator ( == or === ) actually tests for equality.
JS Comparison operators
For example:
The
=
is an assignment operation.The
!=
is an inequality operator.The
==
is an equality operator.I guess what you need is the
==
operator. So replace your code with:It's because simple "=" is not for comparaison. Use "==" instead.