-1

I have this html::

<div>
<h1>This is heading</h1>
</div>

I want to store this html in local storage as it is.

But if I try to store this html in localstorage as it is, It stores something like this::

<div><h1>This is heading</h1></div>

Is there any solution??

  • 3
    The two are functionally identical – Andrew Aug 25 '19 at 14:15
  • _"I have this html::"_ You must be referring to the representation of DevTools. Bo the Markup shown above are identical. – Rayon Aug 25 '19 at 14:19
  • Show us the code that stores and retrieves the html to/from localstorage. Also, do you get the vaue from the DOM or something? Where do the linebreaks in the original come from? – Bergi Aug 25 '19 at 14:19

1 Answers1

2

You can do so in template strings in JavaScript.

code example below:

var html = `<div>
<h1>This is heading</h1>
</div>`;

localStorage.setItem('html', html)

Getting the item returns the string in multiline.

localStorage.getItem('html')
 "<div>
 <h1>This is heading</h1>
 </div>"
koushikmln
  • 648
  • 6
  • 23
  • I think the real question is how you generate that template string to begin with. I'm assuming it needs to be dynamic while reading it off the DOM. You need some type of algorithmic parser to construct it – Andrew Aug 25 '19 at 20:10
  • This is assuming he already has the html generated which is mentioned in the question. How you generate this or read it from the dom would depend on the application logic and the html he wants to generate. – koushikmln Aug 26 '19 at 07:19