Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving an array in localstorage

Hi,
i need to save every address a user enters into a text input in local storage as an array, Have you any details on this.

thank you

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

saving an array in localstorage

Here is the code which saves element item to varName variable

function addItemToArray(item, varName) { var arr = JSON.parse(localStorage.getItem(varName)); if(arr == null) {arr = new Array();} arr.push(item); localStorage.setItem(varName, JSON.stringify(arr))}

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving an array in localstorage

thanks marina,
do i run the script on click "save to favourites"

Image

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

saving an array in localstorage

Yes

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving an array in localstorage

Hi Marina,
I've done something wrong,
on textinput component I set action on input, set local storage, "favourites, bind to favourites, and ran the following script onclick, save to favourites button

function addItemToArray(item, favourites) { var arr = JSON.parse(localStorage.getItem(favourites)); if(arr == null) {arr = new Array();} arr.push(item); localStorage.setItem(varName, JSON.stringify(arr))}

it only saves the last entry, can tell where I went wrong

thank you for you help.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

saving an array in localstorage

Hello! You should add this code as Custom JS without changes. It's a static code

function save(item, varName) {
var arr = JSON.parse(localStorage.getItem(varName));
if(arr == null) {
arr = new Array();
}
arr.push(item);
localStorage.setItem(varName, JSON.stringify(arr))
}

function myload(varName) {
var arr = JSON.parse(localStorage.getItem(varName));
if(arr == null) {
arr = new Array();
}
return arr;
}

Then to save an array on button click run next code

save(Tiggzi('component_name').text(), 'variable_name')

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving an array in localstorage

Thanks Marina,

I have included the static code in create new java,

as yesterday, the text input id is "favourites", the click button id is "favourites button".

On input event I set local storage variable, and bound favourites to favourites (text)

on favourites button I set event onclick run custom java and used the code you gave me yesterday

function addItemToArray(item, favourites) { var arr = JSON.parse(localStorage.getItem(favourites)); if(arr == null) {arr = new Array();} arr.push(item); localStorage.setItem(varName, JSON.stringify(arr))}

I have this other code you gave me today

save(Tiggzi('component_name').text(), 'variable_name')

do I run only one code onclick or both

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

saving an array in localstorage

Try to use only this code save(Tiggzi('component_name').text(), 'variable_name')

Change component names and variable names

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving an array in localstorage

super cool Marina,
this is the result from localstorage,

variable_favourites "["","","","","","",""]"

is there any docs on how to populate a selectmenu with this array

kind regards

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

saving an array in localstorage

Hi Marina,
this is the result in local storage in firebug, each time I save to favourites I get ,"" added to localstorage variables_favourites, but no text
I thought that the text would be there, is this correct?

variable_favourites "["","","","","","",""]"

Return to “Issues”