The input checkbox toggles between light/dark mode. However, I want it as 2 separate buttons. How do I go about this?
Demo: https://jsfiddle.net/ot1ecnxz/1
HTML:
<input type="checkbox" role="switch" id="flexSwitchCheckChecked" checked onclick="myFunction()" />
<hr />
<button type="button" data-bs-theme-value="light"> Light </button>
<button type="button" data-bs-theme-value="dark"> Dark </button>
<hr />
<p>
This is a sample text
</p>
JS:
function myFunction() {
var element = document.body;
element.dataset.bsTheme =
element.dataset.bsTheme == "dark" ? "light" : "dark";
}
2
Answers
If checkbox replaced by 2 dedicated buttons code to achieve this should be,
HTML :
Javascript:
Html
JS
try for this code