Created
April 18, 2023 12:35
-
-
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"
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
<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