Como lembrar se a caixa está marcada ou desmarcada (com um clique) com localstorage e também salva um valor?
Aqui está o jsfiddle:http://jsfiddle.net/nng6L/7/
Então, o que eu quero fazer é o seguinte:
Eu quero definir um valor em localstorage, de1
se a caixa estiver marcada ou0
se a caixa estiver desmarcadaQuando a página é recarregada, se a caixa foi marcada, ela permanece marcada, após buscar o valor no local storageEu quero ser capaz de exibir um1
ou um0
em texto simples, tendo buscado o valor mencionado no armazenamento local.Aqui está o meu código, mas não está funcionando (quando a página é recarregada, a caixa não está marcada enull
é retornado em vez de um1
ou um0
):
script.js
// here is to set item in localstorage when you click the check box.
vvvvvvv = document.getElementById('xxxx');
vvvvvvv.onclick = function() {
if(document.getElementById('xxxx').checked=true) {
localStorage.setItem('yyyyyyy', "1");
} else {
localStorage.setItem('yyyyyyy', "0");
}
}
// here is to fetch the item stored in local storage, and use that value
// to check or uncheck the box based on localstorage value.
zzzzzzz = localStorage.getItem('yyyyyyy');
if (zzzzzzz === null) {
localStorage.setItem('yyyyyyy', "0");
document.getElementById("xxxx").checked=false;
}
else {
if (zzzzzzz === "1") {
document.getElementById("xxxx").checked=true;
} else if (zzzzzzz === "0") {
document.getElementById("xxxx").checked=false;
}
}
output.js
// here is to output the value to the web page so we can know
// what value is stored in localstorage.
wwwwwwww = localStorage.getItem('yyyyyyy');
document.write(wwwwwwww);
page.html
<!-- here is the check box for which the scripts above are based from -->
<input type="checkbox" id="xxxx">Do you like summer?
<br><br>
<!-- here is where it will display the value from localstorage -->
<script src="output.js">