Eggo
Eggo

Reputation: 539

Jquery .html() not working with localStorage

I first store html code locally using this script:

queuehistory = $("#queue").html();
localStorage.setItem("queuehistory", queuehistory); 

When the page loads, I run a script that detects if the localstorage item has been set:

if (localStorage.getItem("queuehistory"))
{
  $("#queue").html(localStorage.getItem("queuehistory"));
} 

Although localstorage has been set and does exist, for some reason the html code is not being loaded into $("#queue").

For testing purposes, I ran this bit of code:

 if (localStorage.getItem("queuehistory"))
 {
  alert(localStorage.getItem("queuehistory"));  
  $("#queue").html(localStorage.getItem("queuehistory"));
 }

localStorage.getItem is definitely not empty, so I have no idea why this bit of code does not seem to be working. The storage itself works, but loading my locally stored html code into a div does not seem to work. Any help would be greatly appreciated.

Upvotes: 2

Views: 465

Answers (2)

luisZavaleta
luisZavaleta

Reputation: 1168

localStorage only stores strings, you can't store a a Dom element. (it might work with some browsers, but it's not in the specification, so don't use it in that way)

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Place your code in side dom ready callback

$(function(){
    if (localStorage.getItem("queuehistory")){
        alert(localStorage.getItem("queuehistory"));
        $("#queue").html(localStorage.getItem("queuehistory"));
    }
})

Upvotes: 3

Related Questions