Keeping Checkboxes Checked when user Navigates away from your Page
This question is related to code from another user 8 months before this post...
I have been trying to apply this code to my project, but I can't seem to get the code to work. The first time the user makes selections this code is applied after the user has made their choices and a SAVE button is pressed. After this SAVE BUTTON click event...this javascript runs...
--------------------------------------------------------------------------------// Hello! Save checked checkboxes into localStorage. On page Show restore them // from localStorage. To save...
var checked = [];
Appery("checkboxgroup").find("input:checked").each(function(){
checked.push($(this).attr("value"));
});
localStorage.setItem("mobilecheckboxgroupName_checked", JSON.stringify(checked));
//to restore
var j, len;
Appery("checkboxgroup").find("input").each(function () {$(this).prop("checked", false).checkbox("refresh");});
try {
checked = JSON.parse(localStorage.getItem("mobilecheckboxgroupName_checked"));
if ({}.toString.call(checked) !== "[object Array]") {
checked = [];
}
} catch ( e ) {
checked = [];
}
for (j = 0, len = checked.length; j < len; i++) {
try {
Appery("checkboxgroup").find("input[value='" + checked[j] + "']").prop("checked", true).checkbox("refresh");
} catch ( e ) {
}
}
// To save selected value into localStorage you can use Value change event and Set
// local storage variable action, or the following code
localStorage.setItem("mobilecheckboxgroupName_checked", Appery("checkboxgroup").val());
--------------------------------------------------------------------------------I understand what is above code is doing, but I am not sure if I need to use all the codes (there are 4 parts) to make my app do the same thing.
I have tried this code when the user comes back to the page with checkboxes as an "ON PAGE SHOW" event
------------------------------------------------------------------------------------------------var $select = Appery("checkboxgroup");
$select.find('option[value="' + localStorage.getItem("mobilecheckboxgroupName_checked") + '"]').attr("selected", true);
$select.selectmenu("refresh");
------------------------------------------------------------------------------------------------