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

259
Vistas
¿Por qué mi almacenamiento local para mi botón JS no funciona?

No estoy seguro de por qué mi almacenamiento local no funciona. Cuando se carga la página, carga la función almacenada (), que debería ser el valor almacenado para mi función de alternancia, que cambia la página de clara a oscura.

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>

(Se eliminó el archivo javascript antiguo porque no me permitía publicar demasiado código)

HTML actualizado:

 <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>

JS actualizado:

 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 Respuestas
Responde la pregunta

0

Es input.value y no input.val() en JavaScript vainilla. Y su onclick debería estar en la entrada. Yo lo haría así:

 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 Denunciar
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