Created
January 23, 2021 17:28
-
-
Save mohnishlandge/231880083098a7230263a0cca74f6fc7 to your computer and use it in GitHub Desktop.
Global Dark-Mode Switch (JS & Query)
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
// © Mohnish Landge | |
// [Creative Coding Challenges] #001 | |
// HTML Direct ID Dark-Mode Toggle | |
var root = document.getElementsByTagName('html')[0]; | |
root.className += ' myCssClass'; | |
let darkMode = localStorage.getItem('darkMode'); | |
function enableDarkMode() { | |
document.getElementsByTagName('html')[0].classList.add('darkmode'); | |
localStorage.setItem('darkMode', 'enabled'); | |
} | |
function disableDarkMode() { | |
document.getElementsByTagName('html')[0].classList.remove('darkmode'); | |
localStorage.setItem('darkMode', null); | |
} | |
if (darkMode === 'enabled') { | |
enableDarkMode(); | |
} | |
// Listeners | |
const darkModeToggle = document.querySelector('#dark-mode-toggle'); | |
darkModeToggle.addEventListener('click', () => { | |
darkMode = localStorage.getItem('darkMode'); | |
darkMode !== 'enabled' ? enableDarkMode() : disableDarkMode(); | |
}); | |
// © Mohnish Landge | |
// First learn the code you'll actually use in your projects. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment