javascript - keeping checkbox status in rails -
i have check-boxes in rails application code below
<%= check_box_tag "audio_ids[]",audio.id ,nil,class: "song"%> <%= check_box_tag "audio_ids[]",audio.id ,nil,class: "song"%> <%= check_box_tag "audio_ids[]",audio.id ,nil,class: "song"%> <button class="btn btn-warning">play all</button> <script> function handlebuttonclick(button){ if ($(button).text().match("check all")){ $(":checkbox").prop("checked", true) } else { $(":checkbox").prop("checked", false) }; updatebuttonstatus(); } function updatebuttonstatus(){ var allchecked = $(":checkbox").length === $(":checkbox:checked").length; $("button").text(allchecked? "uncheck all" : "check all"); } function updatecookie(){ var elementvalues = {}; $(":checkbox").each(function(){ elementvalues[this.id] = this.checked; }); elementvalues["buttontext"] = $("button").text(); $.cookie('elementvalues', elementvalues, { expires: 7, path: '/' }) } function repopulateformelements(){ var elementvalues = $.cookie('elementvalues'); if(elementvalues){ object.keys(elementvalues).foreach(function(element) { var checked = elementvalues[element]; $("#" + element).prop('checked', checked); }); $("button").text(elementvalues["buttontext"]) } } $(":checkbox").on("change", function(){ updatebuttonstatus(); updatecookie(); }); $("button").on("click", function() { handlebuttonclick(this); updatecookie(); }); $.cookie.json = true; repopulateformelements(); </script>
they keep status while refreshing page whenever came different page lost status when check code separately works not rails , problem code , have do?
Comments
Post a Comment