Skip to content

Instantly share code, notes, and snippets.

@aeruo
Created September 8, 2023 10:57
Show Gist options
  • Save aeruo/98d36127bea58c5c31ce26cd50d63150 to your computer and use it in GitHub Desktop.
Save aeruo/98d36127bea58c5c31ce26cd50d63150 to your computer and use it in GitHub Desktop.
Jquery script for dark mode toggle
$(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