Displaying array values from local storage
I am trying to display the values that have been stored in an array in local storage. I was able to save the values to local storage and add other values with the click of a button, but I cannot get the values back and display them on the page.
This is my code adding values to local storage.
$("#player1Link").click(function() {
if (localStorage.getItem("player1Favourite") !== null) {
var a = JSON.parse(localStorage.getItem('player1Favourite'));
if (a.indexOf(chosenPlayerId) === -1) {
a.push(chosenPlayerId);
localStorage.setItem('player1Favourite', JSON.stringify(a));
alert("Pushed in");
} else {
alert("Already in favourites");
}
} else {
var a = [];
a.push(chosenPlayerId);
localStorage.setItem('player1Favourite', JSON.stringify(a));
}
})
I want you to be able to click on the button to get the values and display them on the page, but I can find the code for this function. $("#playerRetrieve").click(function() {});
If anyone can help it would be greatly appreciated.
source to share
I did a jsfiddle, it looks like it works there: jsfiddle
try:
localStorage.getItem('player1Favourite');
or
localStorage.player1Favourite
You might want to look at: this section or in Mozilla
source to share
I'm not entirely sure if I understand you correctly, because if you just want to get the values, you can use the same code, just remove the embedding from your code and change the jQuery selector.
$("#playerRetrieve").click(function() {
if (localStorage.getItem("player1Favourite") !== null) {
var a = JSON.parse(localStorage.getItem('player1Favourite'));
// Do what you want with the values, which are now in a.
}
});
source to share