I'm trying to keep the checkboxes selected even after refresh on the page, this is my progress:
// javascript
var s_item = "";
function save(items) {
s_item = items;
var checkbox = document.getElementById(s_item);
localStorage.setItem(s_item, checkbox.checked);
return s_item;
}
function load() {
if (s_item != "") {
var checked = JSON.parse(localStorage.getItem(s_item));
document.getElementById(s_item).checked = checked;
}
}
load();
// html
<input id="@item.Id" type="checkbox" onclick="save('@item.Id');">
In the checkbox, I am trying to pass as an id parameter of each checkbox I have with a onclick()
event and writes them to localStorage
, I do not know if this is the best way to solve my problem however, right because it is giving error with the global variable "s_item" is always as null
.
I'm using foreach
to checkboxes in asp.net mvc
.
I basically just need these checked checkboxes to remain selected after a reload on the page.