I need to keep track of a list of 25 favourites which are added from a list of ~100 entries. These favourites would be in a list of say
- "favourite 1"
- "favourite 2"
- "favourite 3" .... so on
and I need them to be stored persistently. I also would require them to be able to be deleted and replaced with another favourite string value. I have looked at How do I store an array in localStorage? but that doesn't work for me because when I declare var names=[];
on the javascript file, every time my favourites function runs it redeclares the array and clears everything.
I have tried doing something like:
function addToFav(string)
{
if (localStorage.fav1) // if fav1 was created before
{
alert("local storage fav1 present, don't make variables");
}
else
{
alert('fav variables never made, make them now');
var i=1;
for (i=1; i<=25; i++)
{
var favNumber = "fav" + i;
localStorage.favNumber = "x";
}
alert(localStorage.fav1); // outputs "undefined"
}
}
it was my intention to make localStorage variables of fav1
,fav2
,fav3
...fav25
and then manage them individually. But this doesn't work since calling localStorage.favNumber = "x";
makes the local storage variable favNumber
equal to "x"
and not fav+i
.
I'm out of ideas at the moment; I have looked at http://playground.html5rocks.com/#async_transactions to try using a HTML5 database but I'm very new to web development and it seems a bit too much for what I'm trying to do. Does anyone know how to fix this issue? Any information would be helpful.