Answers for "toggle dark mode javascript"

0

toggle dark mode javascript

<style>
  body {
  background-color: white;
  color: black;
}

/* DARK MODE */
.dark-mode{
  background-color: black;
  color: white;
} 
</style>
  <button class="nav-item"><a role="button" onclick="darkmode()">Toggle Me</a></button>

</script>
function darkmode() {
    const wasDarkmode = localStorage.getItem('darkmode') === 'true';
    localStorage.setItem('darkmode', !wasDarkmode);
    const element = document.body;
    element.classList.toggle('dark-mode', !wasDarkmode);
  }

  function onload() {
    document.body.classList.toggle('dark-mode', localStorage.getItem('darkmode') === 'true');
  }</script>
Posted by: Guest on July-04-2021

Browse Popular Code Answers by Language