Skip to content

Instantly share code, notes, and snippets.

@Vijaysinh
Created November 19, 2020 10:23
Show Gist options
  • Save Vijaysinh/2baa3ae72c2df766f81b46b87383f27e to your computer and use it in GitHub Desktop.
Save Vijaysinh/2baa3ae72c2df766f81b46b87383f27e to your computer and use it in GitHub Desktop.
toggle-theme-js
function setTheme(themeName) {
localStorage.setItem('theme', themeName);
document.documentElement.className = themeName;
}
// function to toggle between light and dark theme
function toggleTheme() {
if (localStorage.getItem('theme') === 'theme-dark') {
setTheme('theme-light');
} else {
setTheme('theme-dark');
}
}
// Immediately invoked function to set the theme on initial load
(function () {
if (localStorage.getItem('theme') === 'theme-dark') {
setTheme('theme-dark');
document.getElementById('slider').checked = false;
} else {
setTheme('theme-light');
document.getElementById('slider').checked = true;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment