Reputation: 451
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
Reputation: 5664
The correct syntax for setItem
is
localStorage.setItem("GetData", value)
not
localStorage.setItem = ("GetData", value)
Upvotes: 3
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
Reputation: 140210
Try this
if( window.localStorage ) {
alert(value);
localStorage.setItem("GetData" , value);
}
Upvotes: 1