srinu
srinu

Reputation: 451

How can I get the local Storage Value

var cookieValue = document.getElementById("demo");      
var value = cookieValue .getAttribute('value');

if(typeof(Storage)!=="undefined")
{
  alert(value);
  localStorage.setItem = ("GetData" , value);
  alert(localStorage.setItem);
}

function loading()
{
  alert("coming");
  var allcookies = localStorage.getItem('GetData');
  alert(allcookies);
}   

Above is the way I am setting localStorage.setItem and I am getting LocalStorage. But Where I didn't get the output it is showing Null. Please Suggest me any Solution.

Upvotes: 11

Views: 72826

Answers (3)

Brant Olsen
Brant Olsen

Reputation: 5664

The correct syntax for setItem is

localStorage.setItem("GetData", value)

not

localStorage.setItem = ("GetData", value)

Upvotes: 3

Jerome Cance
Jerome Cance

Reputation: 8183

Your code should be :

var cookieValue = document.getElementById("demo");      
    var value = cookieValue .getAttribute('value');
    if(typeof(Storage)!=="undefined")
    {
        alert(value);
        localStorage.setItem("GetData" , value);
        alert(localStorage.getItem("GetData"));

    }

function loading()
{
        alert("coming");
        var allcookies = localStorage.getItem('GetData');
        alert(allcookies);

}

OR

var cookieValue = document.getElementById("demo");      
    var value = cookieValue .getAttribute('value');
    if(typeof(Storage)!=="undefined")
    {
        alert(value);
        localStorage["GetData"] = value;
        alert(localStorage["GetData"]);

    }

function loading()
{
        alert("coming");
        var allcookies = localStorage["GetData"];
        alert(allcookies);

}

Upvotes: 18

Esailija
Esailija

Reputation: 140210

Try this

if( window.localStorage ) {
    alert(value);
    localStorage.setItem("GetData" , value);
}

Upvotes: 1

Related Questions