Reputation: 1
In bootstrap, I want to make a 'darkmode' toggle using the light/dark mode button.
<button type="button" class="btn btn-dark">Dark Mode</button>
press this button
class="btn btn-dark" --> class="btn btn-light"
As the class changes
function darkMode() {
var body = document.body;
body.classList.toggle("dark-mode");
var button = document.getElementById("button");
if (button.innerHTML == "Dark Mode") {
button.innerHTML = "Light Mode"
} else {
button.innerHTML = "Dark Mode";
button.innerText
}
}
I've made it up to here.
I want to change the class in bootstrap, how do I do that?
Upvotes: 0
Views: 303
Reputation: 11080
You can use .className
like you did before. For example:
function darkMode(){
var body = document.body;
body.classList.toggle("dark-mode");
var button = document.getElementById("button");
if(button.innerHTML == "Dark Mode"){
button.innerHTML = "Light Mode";
button.className = "btn btn-light";
} else {
button.innerHTML = "Dark Mode";
button.className = "btn btn-dark";
}
}
Upvotes: 1
Reputation: 488
If .toggle() is not working, can also use .addClass() and .removeClass(). Keep a boolean of the toggle value, remove the existing class, and add the class that corresponds to the boolean.
Upvotes: 0