javascript - localStorage save HTML Clone -


i want use localstorage save object contains html-clones.

var myobject["test"] = document.getelementbyid("someelement").clonenode(true); myobject["test2"] = document.getelementbyid("someotherelement").clonenode(true); localstorage.saveobject = json.stringify(myobject); 

but object saveobject still equals {}. seems, stringify can't stringify html node , if, in way, happen solve problem save it, possible parse object html node?

someone knows solution?

your node references removed stringified object because json, definition, cannot contain functions or node references - primitives or sub-arrays/objects. in other words, there's no way can keep references nodes in local storage. instead need log references them id, class or other means.

[edit, in response op's comment]

json , js objects not same thing. former derived latter, not same. json storage means, , cannot contain references dynamic. html elements not exist permanently; exist runtime (dom) concepts disappear once page left. thus, cannot stored in meaningful way.

json therefore able store primitive data - strings, numbers , booleans - along structures allow nest - arrays , sub-json definitions.

so happens when run stringify on object unsuitable parts stripped out. in case, that's both properties. instead, need store references elements in more permanent, revisitable format - id or class, or other reminder mechanism.

var obj = {el1: '#some_element', el2: '.some_other_element'}; localstorage.saveobject = json.stringify(obj); 

there save 2 elements references ids (first) , class (second).

then, when come reload local storage, elements based on factors, feeding them, example, jquery selector.


Comments