samedi 20 février 2016

Keep last checkbox checked after page refresh

I have 6 checkboxes, and I need the last one that gets checked, to stay checked after a page refresh.

What I have now is keeping all the checked checkboxes, checked after page refresh.

Any ideas on how do I edit it to achieve the needed result?

Here's my current code:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Persist checkboxes 1</title>
  </head>

  <body>

      <input type="checkbox" id="option1">

      <input type="checkbox" id="option2">

      <input type="checkbox" id="option3">

      <input type="checkbox" id="option4">

      <input type="checkbox" id="option5">

      <input type="checkbox" id="option6">


    <script src="http://ift.tt/183v7Lz"></script>
    <script src="http://ift.tt/1raaURo"></script>

    <script>
      $("#option").on("change", function(){
        var checkboxValues = {};
        $("#option").each(function(){
          checkboxValues[this.id] = this.checked;
        });
        $.cookie('checkboxValues', checkboxValues, { expires: 1, path: '/' })
      });

      function repopulateCheckboxes(){
        var checkboxValues = $.cookie('checkboxValues');
        if(checkboxValues){
          Object.keys(checkboxValues).forEach(function(element) {
            var checked = checkboxValues[element];
            $("#" + element).prop('checked', checked);
          });
        }
      }

      $.cookie.json = true;
      repopulateCheckboxes();
    </script>
  </body>
</html>




Aucun commentaire:

Enregistrer un commentaire