Created
September 8, 2023 10:57
-
-
Save aeruo/98d36127bea58c5c31ce26cd50d63150 to your computer and use it in GitHub Desktop.
Jquery script for dark mode toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
// Check local storage for user's theme preference | |
const currentTheme = localStorage.getItem('theme'); | |
if (currentTheme) { | |
$('#theme-stylesheet').attr('href', currentTheme); | |
} | |
// Toggle dark mode when the checkbox changes | |
$('#dark-mode-toggle').change(function () { | |
if ($(this).is(':checked')) { | |
// Set the dark theme | |
$('#theme-stylesheet').attr('href', 'dark.css'); | |
// Store the theme preference in local storage | |
localStorage.setItem('theme', 'dark.css'); | |
} else { | |
// Set the light theme | |
$('#theme-stylesheet').attr('href', 'light.css'); | |
// Store the theme preference in local storage | |
localStorage.setItem('theme', 'light.css'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment