Skip to content

Instantly share code, notes, and snippets.

@philliproth
Created April 18, 2023 12:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philliproth/6f94201a0716b00176141d3e295efb63 to your computer and use it in GitHub Desktop.
Save philliproth/6f94201a0716b00176141d3e295efb63 to your computer and use it in GitHub Desktop.
Klasse "dark" zu body hinzufügen bei Klick auf Button mit class="mode-changer"
<script>
const darkBtn = document.querySelector('.mode-changer');
const bodyEl = document.querySelector('body');
const darkMode = () => {
bodyEl.classList.toggle('dark')
}
darkBtn.addEventListener('click', () => {
// Get the value of the "dark" item from the local storage on every click
setDarkMode = localStorage.getItem('dark');
if(setDarkMode !== "on") {
darkMode();
// Set the value of the itwm to "on" when dark mode is on
setDarkMode = localStorage.setItem('dark', 'on');
} else {
darkMode();
// Set the value of the item to "null" when dark mode if off
setDarkMode = localStorage.setItem('dark', null);
}
});
// Get the value of the "dark" item from the local storage
let setDarkMode = localStorage.getItem('dark');
// Check dark mode is on or off on page reload
if(setDarkMode === 'on') {
darkMode();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment