I have a site where users can create divs with a button, and then add text to them. I could save the text to the local storage, but I’d also like to save the divs and their state, like where they are positioned etc. so they stay the same after a refresh. As far as I’m aware, divs cannot be stored in the local storage in the same way. Any ideas?
2
Answers
If
localStorage
is what you want to use, you could serialize the state of each div to an object, and store that object (or an array of objects) into localStorage usingJSON.stringify
and convert it back into the object/array form withJSON.parse
.You wouldnt store the div itself in local storage. Instead of an array of strings, like
Start to think about an array of objects:
You can then convert this array to JSON and store that in local storage instead.