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

255
Visualizações
Why is my localstorage for my JS button not working?

I'm not sure why my localstorage isn't working. When the page loads, it loads up the function stored(), which should be the stored value for my toggle function, which changes the page from light to dark.

HTML:

<body onload="stored()">
<label for="ID_HERE" class="toggle-switchy" >
    <input checked type="checkbox" name="server" id="ID_HERE">
        <span class="toggle" onclick="toggle()" id="saveDarkLight"></span>
      <span class="switch"></span>
    </span>
</label>

(Removed old javascript file as it did not let me post too much code)

Updated HTML:

<label for="ID_HERE" class="toggle-switchy" >
    <input checked onclick="toggle()" type="checkbox" name="server" id="ID_HERE">
        <span class="toggle" id="saveDarkLight"></span>
      <span class="switch"></span>
    </span>
</label>

Updated JS:

function stored() {
    var storedValue = localStorage.getItem("server");
    if(storedValue){
      lightmode()
      document.getElementById("ID_HERE").checked = true;
    }else{
       darkmode()
       document.getElementById("ID_HERE").checked = false;
    }
  }


  function toggle() {
    if(document.getElementById("ID_HERE").checked) {
      lightmode()
      var input = document.getElementById("ID_HERE");
      localStorage.setItem("server", input.checked);
    }
    else {
        darkmode()
        var input = document.getElementById("ID_HERE");
        localStorage.setItem("server", input.checked);
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is input.value and not input.val() in vanilla JavaScript. And your onclick should be on the input. I would do it like so :

function stored() {
    var storedValue = localStorage.getItem("server");
    if(sotoredValue){
      lightmode()
      document.getElementById("ID_HERE").checked = true;
    }else{
       darkmode()
       document.getElementById("ID_HERE").checked = false;
    }
  }


  function toggle() {
    if(document.getElementById("ID_HERE").checked) {
      lightmode()
      var input = document.getElementById("ID_HERE");
      localStorage.setItem("server", input.checked);
    }
    else {
        darkmode()
        var input = document.getElementById("ID_HERE");
        localStorage.setItem("server", input.checked);
    }
}

function darkmode() {
  const bodyChanges = document.querySelectorAll('.margin_body');
  for (let i = 0; i < bodyChanges.length; i++) {
    bodyChanges[i].style.background = '#0c0a0f';
  }
}

function lightmode() {
  const bodyChanges = document.querySelectorAll('.margin_body');
  for (let i = 0; i < bodyChanges.length; i++) {
    bodyChanges[i].style.background = 'white';
  }
}
<body onload="stored()">
  <label for="ID_HERE" class="toggle-switchy" >
      <input checked onclick="toggle()" name="server" id="ID_HERE">
  </label>
</body>
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