javascript - remain same clicked checkbox result when page refreshing while highlighting table record -
the following code written accomplish 2 function 1. highlight table record when checkbox clicked 2. keep results same eventhough page refreshed code neither table record highlighted or nor page keep result same when refreshed. posted same question earlier also, people suggest me on localstorage. tried also. knowledge poor couldn't understand explanation given completely. highly appreciated have submit university assignment
<style> #cb3.highlight .label {background-color:yellow;} #cb2.highlight .label {background-color:green;} #cb1.highlight .label {background-color:red;} } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> array.prototype.remove = function() { var what, = arguments, l = a.length, ax; while (l && this.length) { = a[--l]; while ((ax = this.indexof(what)) !== -1) { this.splice(ax, 1); } } return this; }; var checked = []; $(document).ready(function() { if (localstorage.getitem("checked") == null) localstorage.setitem("checked", checked); $("#table input").click(function() { if ($(this).is(":checked")) { $(this).parent().parent().addclass("cb3.highlight .label "); checked.push($(this).attr("cb3")); } else { $(this).parent().parent().removeclass("cb3.highlight .label "); checked.remove($(this).attr("cb3")); } localstorage.setitem("checked", json.stringify(checked)); }); var saved = json.parse(localstorage.getitem("checked")); (var = 0;i < saved.length; i++) { var itematindex = $("#" + saved[i] + ""); itematindex.click(); itematindex.parent().parent().addclass("cb3.highlight .label "); } }); $(document).ready(function() { if (localstorage.getitem("checked") == null) localstorage.setitem("checked", checked); $("#table input").click(function() { if ($(this).is(":checked")) { $(this).parent().parent().addclass("cb2.highlight .label "); checked.push($(this).attr("cb2")); } else { $(this).parent().parent().removeclass("cb2.highlight .label "); checked.remove($(this).attr("cb2")); } localstorage.setitem("checked", json.stringify(checked)); }); var saved = json.parse(localstorage.getitem("checked")); (var = 0;i < saved.length; i++) { var itematindex = $("#" + saved[i] + ""); itematindex.click(); itematindex.parent().parent().addclass("cb2.highlight .label "); } }); $(document).ready(function() { if (localstorage.getitem("checked") == null) localstorage.setitem("checked", checked); $("#table input").click(function() { if ($(this).is(":checked")) { $(this).parent().parent().addclass("cb1.highlight .label "); checked.push($(this).attr("cb1")); } else { $(this).parent().parent().removeclass("cb1.highlight .label "); checked.remove($(this).attr("cb1")); } localstorage.setitem("checked", json.stringify(checked)); }); var saved = json.parse(localstorage.getitem("checked")); (var = 0;i < saved.length; i++) { var itematindex = $("#" + saved[i] + ""); itematindex.click(); itematindex.parent().parent().addclass("cb1.highlight .label"); } }); </script> <div class="col-lg-10"> <table id="table" border="1"> <tr id="cb1"> <td><input type="checkbox" name="cb1" value="y" /></td> <td class=label>click me</td> </tr><tr id="cb2"> <td><input type="checkbox" name="cb2" value="y" /></td> <td class=label>click me</td> </tr> <tr id="cb3"> <td><input type="checkbox" name="cb3" value="y" /></td> <td class=label>click me</td> </tr> </table> </div>
few things need fixed: have repeated same piece of code thrice, if doing because 3 checkboxes, not required. have used selector select 3 checkboxes , attach event handler click.
you adding classes wrong, give names classes added 'addclass()' method
$(this).attr("cb3")
, wrong, want use $(this).attr("name")
.
<!doctype html> <html> <body> <style> #cb3.highlight.label {background-color:yellow;} #cb2.highlight.label {background-color:green;} #cb1.highlight.label {background-color:red;} } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> array.prototype.remove = function() { var what, = arguments, l = a.length, ax; while (l && this.length) { = a[--l]; while ((ax = this.indexof(what)) !== -1) { this.splice(ax, 1); } } return this; }; $(document).ready(function() { var checked = []; $("#table input").click(function() { if ($(this).is(":checked")) { $(this).parent().parent().addclass("highlight label"); checked.push($(this).attr("name")); } else { $(this).parent().parent().removeclass("highlight label "); checked.remove($(this).attr("name")); } localstorage.setitem("checked", json.stringify(checked)); }); if (localstorage.getitem("checked") !== null){ var saved = json.parse(localstorage.getitem("checked")); for (var = 0;i < saved.length; i++) { $("[name='" + saved[i] + "']").trigger('click'); } } }); </script> <div class="col-lg-10"> <table id="table" border="1"> <tr id="cb1"> <td><input type="checkbox" name="cb1" value="y" /></td> <td class=label>click me</td> </tr><tr id="cb2"> <td><input type="checkbox" name="cb2" value="y" /></td> <td class=label>click me</td> </tr> <tr id="cb3"> <td><input type="checkbox" name="cb3" value="y" /></td> <td class=label>click me</td> </tr> </table> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> </script> </body> </html>
Comments
Post a Comment