You don’t 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 which remain even if the user closes the browser (more info on localStorage
at this link).
You can create a Listener in jQuery for the checkbox
which will create (or delete the localStorage
):
$("#id_do_checkbox").on("click", function(){
if($(this).prop("checked")){
localStorage.marcado = true;
}else{
localStorage.removeItem("marcado");
}
});
And make a check if the localStorage
exists, marking the checkbox
automatically on page loading:
$(document).ready(function(){
if(localStorage.marcado){
$("#id_do_checkbox").prop("checked", true);
}
});
In the 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);
}
}
});
Awesome Thank you again!!
– Shider
and in case I’m listing the check boxes with a while cycle since I’m going to get the information from the database?
– Shider
yes I have a checkbox for each task
– Shider
@Shider if there are several, then complicates more... but gives me a moment that put a solution in the answer.
– Sam
@Shider Are checkboxes separated by Divs? Each within a specific div?
– Sam
has these 2 but on the outside has the while cycle or will make this instruction the number of times you have in the database
– Shider
@Shider updated the answer.
– Sam
Top Thanks and sorry for the time you lost!
– Shider
Let’s go continue this discussion in chat.
– Sam
@Shider Thanks! Abs!
– Sam