I would like to enable a dark mode
feature on my website by using a toggle button
. I already got dark mode
to work now I would like to have the button say Turn on dark mode
when it is off and then Turn off dark mode
when it is on.
function myFunction() {
var element = document.body;
element.classList.toggle("dark-mode");
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
background-color: white;
color: black;
}
.dark-mode {
background-color: black;
color: white;
}
<button onclick="myFunction()">Turn on dark mode</button>
function myFunction() {
var element = document.body;
var btn = document.getElementById("modeSwitcher");
element.classList.toggle("dark-mode");
if(element.classList.contains("dark-mode"))
btn.innerHTML= "Turn off dark mode";
else
btn.innerHTML= "Turn on dark mode";
}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
background-color: white;
color: black;
}
.dark-mode {
background-color: black;
color: white;
}
<button onclick="myFunction()" id="modeSwitcher">Turn on dark mode</button>