You do not need to use a database for this. You can save this information using JavaScript localStorage
.
The
localStorage
works similar to a cookie , which you can use to store information to be used later, and that remain even if the user closes the browser (more info about
localStorage
< a href="https://developer.mozilla.org/en/docs/Web/API/Window/Window.localStorage"> this link ).
You can create a listener in jQuery for checkbox
that will create (or delete localStorage
):
$("#id_do_checkbox").on("click", function(){
if($(this).prop("checked")){
localStorage.marcado = true;
}else{
localStorage.removeItem("marcado");
}
});
And make a check if localStorage
exists by automatically% checking% on page load:
$(document).ready(function(){
if(localStorage.marcado){
$("#id_do_checkbox").prop("checked", true);
}
});
In case of multiple checkbox
$("input[type='checkbox']").on("click", function(){
var box = $("input[type='checkbox']");
if($(this).prop("checked")){
localStorage.setItem('marcado'+box.index(this), true);
}else{
localStorage.removeItem("marcado"+box.index(this));
}
});
$(document).ready(function(){
var boxes = $("input[type='checkbox']");
for(var x = 0; x < boxes.length; x++){
if(localStorage.getItem("marcado"+x)){
$(boxes[x]).prop("checked", true);
}
}
});