Skip to content

Instantly share code, notes, and snippets.

<body class="darkmode">
<header>
<button class="color-switch">Light</button>
</header>
<h1>CSS Variable Dark Mode Switcher</h1>
</body>
const button: HTMLButtonElement = document.querySelector(".color-switch");
button.addEventListener("click", function() {
const body: HTMLBodyElement = document.querySelector("body");
body.classList.toggle("lightmode");
body.classList.toggle("darkmode");
if (button) {
button.innerText = body.classList.contains("lightmode") ? "Dark" : "Light";
}
const button = document.querySelector(".color-switch");
button.addEventListener("click", function() {
const body = document.querySelector("body");
body.classList.toggle("lightmode");
body.classList.toggle("darkmode");
if (button) {
button.innerText = body.classList.contains("lightmode") ? "Dark" : "Light";
}