javascript - How to store checkbox and array values (cookies) -
javascript - How to store checkbox and array values (cookies) -
i want save boolean values of checkbox longer period. need store values of each checkbox in array read them out later. i´ve tried illustration code , tutorials cookies nil seems work.
with checklist_1s.html access checklistrequest.js store values of each checkbox in array. later i´ll need utilize array js function in window, moment open window, array gone.
i´m looking easy solution here, if possible.
note: know using same name every checkbox violation, seems work far , couldn´t find other way work every checkbox (i´ll need 200 later)
checklist_1s.html
<html> <head> <link rel="stylesheet" type="text/css" href="../../style.css"></link> <script src="checklistrequest.js" type="text/javascript"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script> <script src="https://raw.github.com/andris9/jstorage/master/jstorage.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="http://cdn.jsdelivr.net/jquery.cookie/1.4.0/jquery.cookie.min.js"></script> <style> </style> </head> <body class="main"> <div class="main_2"> <p> <h1>checklist</h1> <form> <input type="checkbox" name="remember" id="remember" onclick="checkboxfunction(value, 0)" value="f-card geholt">f-card geholt<br> <input type="checkbox" name="remember" id="remember" onclick="checkboxfunction(value, 1)" value="ersti rally">ersti rally <br> <input type="checkbox" name="remember" id="remember" onclick="checkboxfunction(value, 2)" value="test">test<br> </form> <input type="button" name="alert" value="aktualisieren" onclick="alertfunction()"> <!-- illustration code--> <script> $("#checkall").on("change", function(){ $(':checkbox').not(this).prop('checked', this.checked); }); $(":checkbox").on("change", function(){ var checkboxvalues = {}; $(":checkbox").each(function(){ checkboxvalues[this.id] = this.checked; }); $.cookie('checkboxvalues', checkboxvalues, { expires: 7, 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> </p> </div> </body> </html> checklistrequest.js
var checkbox = document.getelementsbyname("remember"); var checkboxarray = []; function checkboxfunction(value, index) { if(checkbox[index].checked == true) { checkboxarray[index] = value; } if (checkbox[index].checked == false && value == checkboxarray[index]) { checkboxarray[index] = null; } } function alertfunction() { for(var i=0; i<99; i++) { if (typeof checkboxarray[i] != 'undefined' && checkboxarray[i] != null) { alert(checkboxarray[i]); } } alert("test"); } javascript jquery html arrays
Comments
Post a Comment