Reputation: 309
I'm trying to make a checklist without jQuery, of which the .checked status of each checkbox is saved in localStorage, so that it can be maintained when the browser or even the computer is restarted.
To achieve this, I've written three functions:
saveStatus()
, to save the current status of all checkboxes to localStorage. Activated by a button onclick.loadStatus()
, to load the saved status from localStorage. Activated onload in the body tag.clearAllInputs()
, to clear localStorage. Activated by a button
onclick.I couldn't find a better workaround for localStorage only accepting ("String","String") key-value pairs.
When I check a couple of the checkboxes and hit the save button, it just seems to refresh the page and all checkboxes are unchecked. Same thing happens when I hit the clear button.
Upvotes: 0
Views: 154
Reputation: 119837
That's where JSON comes in. You can have an object or array store the states of your checkboxes. Once you save to localStorage
, use JSON.stringify
on the object to turn it to a string. Then save that string to localStorage
. Reading goes the other way around, by reading the string from localStorage
then use JSON.parse
to turn the string back to an object or array for use on the checkboxes.
Upvotes: 1