Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Receiving an error on my JS for local storage. Uncaught TypeError: Cannot set properties of null (setting 'checked')

For my javascript I am receiving an error when server = true and then creating a page refresh. It makes me click my lightmode/darkmode button twice before it will change modes. I can not figure out why it can't read checked (occuring at this line in my JS under the stored() function: document.getElementById("ID_HERE").checked = true;)

Error: Uncaught TypeError: Cannot set properties of null (setting 'checked')

HTML:

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

JS:

function stored() {
    const storedValue = localStorage.getItem("server");
    if(storedValue === 'true'){
      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';
  }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda