snipX

Toggling dark mode on & off with Vanilla Js

latorlator
17 days agoHTML
<style>
        .dark {
            background-color: rgb(32, 44, 79);
            color: white;
            transition: all 0.5s ease;
        }
</style>
...
<button class="dark-mode-toggler">Switch mode</button>
...
<script>
    const button = document.querySelector('button.dark-mode-toggler');
    button.addEventListener('click', ()=>{
        document.body.classList.toggle('dark');
    });
</script>