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.

+3


source to share


2 answers


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

+2


source


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.
    }
});

      

+1


source







All Articles