I wrote a simple task list. The javascript code is below, the important part is about localStorage. What i made so far is this: JsBin
What i want to achieve is that when i reload the page, the setting if the entrys should be removed immediatly (if the checkbox next to the textfield is checked or not), is saved and restored from the last visit.
Here is my Javascript/Jquery code:
var anzahl = 0;
var autoremove = true;
var autoremove_backup = localStorage.getItem("autoremove");
console.log(localStorage.getItem("autoremove"));
$(document).ready(function() {
if(autoremove_backup===false){
$("#autoremove").prop( "checked", false);
}
else if (autoremove_backup===true){
$("#autoremove").prop( "checked", true);
}
autoremove = autoremove_backup;
setInterval(entry, 2000);
$("button").on('click', function() {
if(this.id=="add"){
var r = $('<div id="'+ "div"+String(anzahl) +'"><input type="checkbox" id="'+String(anzahl)+'">' + '<label for="'+ String(anzahl)+'" id="'+ "label" +String(anzahl)+'">' + $("#task").val() + '</label><br></div>');
$("#var").append(r);
anzahl = anzahl +1;
}
});
$('input[type=checkbox]').change(
function(){
if (this.checked) {
if(String(this.id)==="autoremove"){
autoremove=true;
saveAutoremove(autoremove);
}
}
else {
if(String(this.id)==="autoremove"){
autoremove=false;
saveAutoremove(autoremove);
}
}
});
});
function entry(){
if(autoremove===true){
$('#var input:checked').each(function() {
$("#div"+String(this.id)).remove();
});
}
}
function saveAutoremove(input){
localStorage.setItem("autoremove", input);
}
Aucun commentaire:
Enregistrer un commentaire