Joris
Joris

Reputation: 309

Maintaining the checked status of checkboxes with localStorage

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.

Fiddle

To achieve this, I've written three functions:

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

Answers (1)

Joseph
Joseph

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

Related Questions