1

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"))
3
  • 1
    localStorage supports only string data Commented Apr 7, 2021 at 16:22
  • Why don't store the id or class of the div element and then retrieve the same and find the corresponding DOM element. Commented Apr 7, 2021 at 16:31
  • read this article it might help. It does not store the html elements themselves but recreates them whenever its loaded again dev.to/michaelburrows/… Commented May 5, 2022 at 12:07

1 Answer 1

1

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.

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

Comments

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.