Saad Hesham
Saad Hesham

Reputation: 95

how to save element created with javascript to local storage

Is it possible to save element to local storage then call it from it this an example for what I'm trying to do

let div = document.createElement("div");

localStorage.setItem("div",div)

document.body.appendChild(localStorage.getItem("div"))

Upvotes: 1

Views: 1517

Answers (1)

Quentin
Quentin

Reputation: 944528

Not as such.

Values in local storage must be strings and DOM elements are not strings.

You would need to serialise the data. Most applications would use JSON and store business data rather then anything that directly represents the rendered view but you could store HTML source code.

Upvotes: 1

Related Questions