By clicking on the button you could add a class to the body which represents your colour. On clicking the button again you can simply check the classList of body.
This quick sample assumes you are not using classes on the body for anything else and that the names of your colours are single worded. If that is not the case, use data-attribute instead.
Edit
As Javascript requested, the mentioned version which does not collide with other potential usecases.
It sets a data-attribute instead of a class and uses styles which regard those data-attributes.
const body = document.querySelector('body')
function myFunction() {
body.style.backgroundColor= "lightblue";
if (body.style.backgroundColor !== '#fff') {
body.style.backgroundColor = '#fff'
}
}
function my1Function() {
body.style.backgroundColor= "lightgrey";
if (body.style.backgroundColor !== '#fff') {
body.style.backgroundColor = '#fff'
}
}
function my2Function() {
body.style.backgroundColor= "pink";
if (body.style.backgroundColor !== '#fff') {
body.style.backgroundColor = '#fff'
}
}
function my3Function() {
body.style.backgroundColor= "lightgreen";
if (body.style.backgroundColor !== '#fff') {
body.style.backgroundColor = '#fff'
}
}
Solution 4 :
You will need to check in what “state” the color is in and then change it accordingly.
you can optimize the code and use more variables but the overall idea is that you should use and if statement to check the “state”.
I’m trying to have a button on my webiste that if you press it the background color will change to blue which i have, but i’m trying to code that if you press it again it’ll change to white again.
function myFunction() {
document.body.style.backgroundColor= "lightblue";
}
function my1Function() {
document.body.style.backgroundColor= "lightgrey";
}
function my2Function() {
document.body.style.backgroundColor= "pink";
}
function my3Function() {
document.body.style.backgroundColor= "lightgreen";
}
I tried using alternatives such as case1, case2, case3 etc.
Comments
Comment posted by Syed Arsalan Hussain
Are you saving the state of color on refresh as well?
Comment posted by tacoshy
this removes all classes from the body including classes that must not be related to this at all.
Comment posted by Lain
Yes it does, as already stated in the answer.
Comment posted by tacoshy
a
Comment posted by Lain
How does this change anything?
Comment posted by i-drink-dirty-code
Oops – fixed it.
Comment posted by Lain
body.style.backgroundColor !== '#fff'
Comment posted by tacoshy
If you use so much repetitive code then you need to over think your logic
Comment posted by JavaScript
How does that solve the asked question?
Comment posted by JavaScript
Setting classes is better practice than setting inline styles.
Comment posted by JavaScript
background
Comment posted by tacoshy
if you want to set a specific color to the bodies background then inline is actually better. The specificity weight ensures that it applies and its cleaner then to create dozens of classes just to cover every possible color. This way you only have to set it in HTML.
Comment posted by JavaScript
I respect your opinion, I just disagree. Still you should set