Ad

Function Does Not Execute When Button Is Pressed A Second Time

- 1 answer

This is the function I'm using.

function beHonest() {
    if (document.getElementById("about").style.opacity = "0") {
        document.getElementById("about").style.opacity = "1";
    } else {
        document.getElementById("about").style.opacity = "0";
    }
}

When I press the button, it calls the function. When I press the button a second time, the else statement does not seem to execute. Why is this?

Ad

Answer

Your test is currently written like an assignation:

if (document.getElementById("about").style.opacity = "0")

You must write:

if (document.getElementById("about").style.opacity == "0")
Ad
source: stackoverflow.com
Ad