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

149
Vistas
Unable to set Checkbox to Checked after refreshing and storing in Local Storage

I am currently facing an issue where I am unable to set a input box to stay checked after a refresh. I have stored the values in the local storage, even though the value is stored in the local storage; the checkbox does not remain checked. Please do help me on this! Thank you

Here is the html code:

      <div style="color: #005e95">
        <input type="checkbox" id="checkboxFullDownload" data-dojo-attach-event="onClick: _test" style="cursor: pointer;"  >
        Full Download
      </div>

Here is the JS code:

_test: function(){
        let checkBox = document.getElementById("checkboxFullDownload")

        if(localStorage.getItem(`${xmlTitle} ID:${this.item._id}`) === "AllowFullDownload"){
          checkBox.setAttribute("checked",true)
          console.log("set to true")
        }

        if(checkBox.checked){
          console.log("Checked")
          localStorage.setItem(`${xmlTitle} ID:${this.item._id}`,"AllowFullDownload")
          checkBox.checked = true;
          }
  
        if(!checkBox.checked){
          console.log("Unchecked")
          localStorage.removeItem(`${xmlTitle} ID:${this.item._id}`)
          checkBox.setAttribute("checked",false)
          }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want to check the checkbox on pageload then localStorage.getItem() must be out of scope of any functions which are executed on certain events.

 let checkBox = document.getElementById("checkboxFullDownload");
_test: function() {
  if (checkBox.checked) {
    console.log("Checked");
    localStorage.setItem(`${xmlTitle} ID:${this.item._id}`, "AllowFullDownload");
    checkBox.checked = true;
  }

  if (!checkBox.checked) {
    console.log("Unchecked");
    localStorage.removeItem(`${xmlTitle} ID:${this.item._id}`);
    checkBox.checked = false;
  }
}
if (localStorage.getItem(`${xmlTitle} ID:${_test.item._id}`) === "AllowFullDownload") {
    checkBox.checked = true;
    console.log("set to true");
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Replace checkBox.setAttribute("checked",true) with checkBox.checked = true

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