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

403
Visualizações
Making a checkbox persistent on page reload?

I've created a clock based on a fantasy timezone. I've then created an "alarm" which will execute an alert if a checkbox is ticked.

I'm trying to use local storage to make the checkbox persistent on page reload, so that people can go to other pages on the website, or refresh without having to tick the box each time.

I can't seem to get it to work though and I've been through lots of similar questions, such as this one: how to Keep checkbox checked after refresh the page

Here is a codepen of the code I have: http://codepen.io/anon/pen/xqRWJM?editors=1111

Here's the html code for the checkbox:

<div id="alert">Alert me when it's night time:
<input id="alertme" type="checkbox"></input>
</div>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I've made a simple pen for you which demonstrates how to save the checkbox state with the LocalStorage API. Hopefully you can adapt this to fit your specific needs.

http://codepen.io/oculusriff/pen/RpoJWQ?

HTML

<input type="checkbox" id="test" /> <label for="test">Test</label>

JavaScript

function App() {}

App.prototype.setState = function(state) {
  localStorage.setItem('checked', state);
}

App.prototype.getState = function() {
  return localStorage.getItem('checked');
}

function init() {
  var app = new App();
  var state = app.getState();
  var checkbox = document.querySelector('#test');

  if (state == 'true') {
    checkbox.checked = true;
  }

  checkbox.addEventListener('click', function() {
      app.setState(checkbox.checked);
  });
}

init();

Cheers, Josh

over 4 years ago · Santiago Trujillo 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