Task List

2

This is the following I am doing a list of tasks and at this moment I am with a doubt wanted to do a checkbox for when the task is completed the person to put checked my question is if it is necessary database for this purpose or not? so that when reloading the page it saves the decision of the person! and if it is with database what is the best way?

<div class="to-do-label">
    <div class="checkbox-fade fade-in-primary">
       <input type="checkbox" id="id_do_checkbox" value="">
    </div>
</div>
    
asked by anonymous 26.12.2017 / 01:50

1 answer

3

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);
      }
   }
});
    
26.12.2017 / 02:17