Page 1 of 2
store and retrieve multiple value from local storage.
Posted: Thu Jan 30, 2014 6:57 am
by pritu patel
I am trying to store multiple value in local storage variable..
how to retrive only one from local sorage and how to compare with inputbox value ?????
cityarray == Mumbai,Pune,Delhi this value are store in local storage and How to retrive it and compare with inputbox value with on click..
store and retrieve multiple value from local storage.
Posted: Thu Jan 30, 2014 8:52 am
by Maryna Brodina
Hello! In localStorage you can store strings only. If you need to store array or object you can save them as JSON string and parse when you retrieve it.
For example to save array prelocalStorage.setItem("arrayName", JSON.stringify(data));/prewhere data - variable which contains array with data. To read prevar arrayFromLS;
try {
arrayFromLS = JSON.parse(localStorage.getItem("arrayName"));
if ({}.toString.call(arrayFromLS) !== "[object Array]") {
arrayFromLS = [];
}
} catch ( e ) {
arrayFromLS = [];
}/pre
store and retrieve multiple value from local storage.
Posted: Thu Jan 30, 2014 11:13 am
by pritu patel
It's working Maryna..thnk u
store and retrieve multiple value from local storage.
Posted: Thu Jan 30, 2014 11:14 am
by pritu patel
now i want to compare with input box value from array value .how to do it???
store and retrieve multiple value from local storage.
Posted: Thu Jan 30, 2014 11:29 am
by pritu patel
var arr = JSON.parse(localStorage.getItem("cityarray"));
try {
if ({}.toString.call(arr) !== "[object Array]")
{
var x = document.getElementsByName("inputcity").value;
alert(x);
}
else
{
alert(arr[0]['male']);
}
} catch ( e ) {
alert("Parse Error. Some wierd data in localstorage...");
}
It's not working....
store and retrieve multiple value from local storage.
Posted: Thu Jan 30, 2014 5:59 pm
by Kateryna Grynko
Hi Pritu,
What exactly doesn't work? What alert is displayed?
store and retrieve multiple value from local storage.
Posted: Fri Jan 31, 2014 7:25 am
by pritu patel
actually I want to
first store array value in localsotrage as above Maryna told me and i got that solution
second I have input box and in that i enter city name i want to retrive it and check with arrary value that i store in local storage..
for example {mumbai,pune,delhi} is my array
for example input box have pune
then the condition will true and city will find and call another service.
if condition flase then i want to open popup dialog and choose city from that and call service.
store and retrieve multiple value from local storage.
Posted: Fri Jan 31, 2014 8:53 am
by pritu patel
store and retrieve multiple value from local storage.
Posted: Fri Jan 31, 2014 12:59 pm
by Maryna Brodina
Hello!
1) prevar arr = JSON.parse(localStorage.getItem("cityarray")); /pre should be inside try..catch
2) Instead predocument.getElementsByName("inputcity").value/pre use preAppery("componentName").val()/pre3) pre {mumbai,pune,delhi}/pre is not json array. Array should look like this pre["mumbai","pune","delhi"]/pre4) To check if there is x value in arr array use preif (arr.indexOf(x) !== -1) {
//we have this value in array
} else {
//there is no such value in array
}/pre
store and retrieve multiple value from local storage.
Posted: Mon Feb 03, 2014 9:41 am
by pritu patel
thnks Maryna...It's working..
but i tried follow code.
var arr = JSON.parse(localStorage.getItem("CityArray"));
try {
if (arr)
{
Code: Select all
var city=localStorage.getItem("c");
if (arr.indexOf(city) !== -1)
{
alert(true);
}
else
{
alert(false);
}
else
{
alert("array null");
}
} catch ( e ) {
alert(e);
}