0

I am trying to use localStorage to save an array of strings. I have a function addToFav(string) which is meant to load the string value into an array which holds program names, or pNames in my case. I'm trying to follow this outline: How do I store an array in localStorage?

localStorage.arrayNum = 0;
var pNames=[];


function addToFav(string)
{
    pNames[localStorage.arrayNum] = string;

    if (localStorage.arrayNum)
    {
        localStorage.arrayNum=Number(localStorage.arrayNum)+1;
    }
    else
    {
        localStorage.arrayNum=1;
    }

    localStorage['pNames']=JSON.stringify(pNames);
    var storedNames=JSON.parse(localStorage['pNames']);

    document.getElementById("testField").innerHTML=storedNames;

}

When I click the button that invokes addToFav(string), the string that I passed into the function is displayed in the testfield div, which is OK. However, launching the function again should add another element to the array but it doesn't, it just repeatedly adds to pNames[0] which is because I think that the pNames and arrayNum are getting redeclared everytime. With this, does anyone know how to prevent this problem? I would like to create an array that adds favourite items when the user invokes my function.

Thanks!

Community
  • 1
  • 1
user1152440
  • 895
  • 2
  • 13
  • 25

2 Answers2

2

It's because you are using pNames as your key each time.

localStorage['pNames']=JSON.stringify(pNames);

You should do something like:

localStorage['pNames'+someVal]=JSON.stringify(pNames);
Ido Green
  • 2,795
  • 1
  • 17
  • 26
  • This looks like a great answer. Might I suggest as a potential improvement: `length = localStorage['pNames'].length; localStorage['pNames'][length]=JSON.stringify(pNames);` – NT3RP Mar 12 '12 at 20:43
  • wouldn`t that create a different key everytime? I'm trying to follow something along the lines of this post: http://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage – user1152440 Mar 13 '12 at 13:27
  • No. If 'someVal' will contain unique information (as a key should) - it will create the same key for a specific value. – Ido Green Mar 14 '12 at 12:59
0

Never access localstorage like a normal object! Use the according functions for that. This also fixes your error in the code ido mentioned;)

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • I'm trying to follow this solution: http://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage , am I doing it correctly? – user1152440 Mar 13 '12 at 16:18
  • Don't do it. It's the wrong way. [I recommend you this link](https://developer.mozilla.org/en/DOM/Storage) about the DOM-Storage API. [Or the official W3C spec](http://dev.w3.org/html5/webstorage/#the-localstorage-attribute). – Christoph Mar 13 '12 at 16:26
  • Alright I'm checking this out now; is there a way to declare the storage keys only once? Because this is for a mobile app so I wouldn't want to reinitialize every time the app is started. – user1152440 Mar 13 '12 at 16:55