Another way to do this:
var button = document.querySelector("button"); var body = document.querySelector("body"); var isOrange = true; button.addEventListener("click", function() { if(isOrange) { body.style.background = "orange"; }else { body.style.background = "none"; } isOrange = !isOrange; });
In a JavaScript file.
/ *****
ATTENTION! Another way is to apply the class to the element we want to change.
The CSS file must have a class with the desired format:
.orange { background: orange; }
In the latter case, in our js file, we only need to make a class application:
var button = document.querySelector("button"); button.addEventListener("click", function() { document.body.classList.toggle("orange"); });
Yours faithfully:)
Jesus romero
source share