Conditioning background color fails

A body tag has the following style:

background-color: rgb(114, 119, 125);

With JavaScript, I want to test what is the background color and change it accordingly.

setTimeout(function() {
    if (document.querySelector("body").style.backgroundColor = "red") {
        document.querySelector("body").style.backgroundColor = "black"
    } else {
        document.querySelector("body").style.backgroundColor = "blue"
    };
}, 1000);

Because rgb(114, 119, 125) is not Red, I would expect to get a Blue background, but instead I get a Black background.

Why do I have this problem and how would you suggest to solve it?

document.querySelector("body").style.backgroundColor = "red"

is an assignment not a comparison.

3 Likes

Oh, yes, thanks, == "red" instead the former = "red" brought me the redemption.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.