0

I have a Fiddle that shows how I get an jq .map() array of the status of the checkboxes on my site. It produces the following object in the console:

 [Object {name="Sunday", status="Checked"}, Object {name="Monday", status="Checked"}, Object     {name="Tuesday", status="UnChecked"}]

The object is saved to local storage. Now, obviously, on next time the user visits the page, I want to pull that object back and parse it to check off the previously checked boxes (yes, I am aware local storage is not ideal, but it is fine for this project). I know how to programmatic check off the checkboxes, but I cannot seem to loop through the saved object.

Here is the code jsfiddle

1 Answer 1

2

Web storage only support string values. Thus, you need to convert your object to a JSON string using JSON.stringify(obj) before saving it.

When you retrieve it, you can parse it back to an object, using either JSON.parse(str) or $.parseJSON(str).

Updated fiddle with example: http://jsfiddle.net/ab316mkr/5/

From MDN:

Note: Keep in mind that everything you store in any of the storages described in this page is converted to string using its .toString method before being stored. So trying to store a common object will result in string "[object Object]" to be stored instead of the object or its JSON representation. Using native JSON parsing and serialization methods provided by the browser is a good and common way for storing objects in string format.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, that is why my attempts to parse failed. Great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.