Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Where should I put the localStorage in this code?

I am trying to make the dark/light mode theme on my site store locally so that it doesnt untoggle every time a new page is opened or the site is refreshed. Where should I put the localStorage.? Or is the whole thing wrong?

const checkbox = document.getElementById('checkbox');
        
        checkbox.addEventListener('change',()=> {
          // change theme of website
          document.body.classList.toggle('dark');
        });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

if you're using vanilla javascript i.e on you html you've loaded your script as below

<html>
...

<script src='/to/scripts.js'></script>
</body>
</html>

Then you can add two functions, one which set the them to localStorage and the other which reads it and make it accessible.

on update the update function sets the new value for the theme, on read the read function reads the stored value even after refresh/reloads as below

<html>
  ....
 <script>
  //for setting
   const toggleTheme = () => {
      if(localStorage.getItem('theme') === 'dark'){
         localStorage.setItem('theme','light')
      }else{
          localStorage.setItem('theme','dark')
       }
   }
  //for reading
   const theme = () => localStorage.getItem('theme') || 'light'
 </script>

 <script src='/to/scripts.js'></script>
 </body>
 </html>
about 4 years ago · Juan Pablo Isaza Relatório

0

Edit (last answer was not solving the problem)

I think you can use DOMContentLoaded event to set the theme from localStorage, when the checkbox changes you set the selected theme on the storage. This way when the page reloads, it will look for the theme and set it, if theme is not set, it will default to light.

Here is a sample:

document.addEventListener("DOMContentLoaded", function(event) {
  const theme = localStorage.getItem('theme');
  if(theme === undefined) theme = 'light';
  document.body.classList.add(theme);
});

const checkbox = document.getElementById('checkbox');

checkbox.addEventListener('change',(e) => {
    const theme = e.target.checked ? 'dark' : 'light';
  localStorage.setItem(theme);
  document.body.classList.add(theme);
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda