Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  136] [ 2]  / answers: 1 / hits: 17096  / 12 Years ago, sat, july 7, 2012, 12:00:00

I have some values that are dynamically stored at localStorage with incremented values like this: localStorage[Value0], localStorage[Value1],....



When I try to access them like this:



JavaScript:



localStorage[Counter]=0;
var i = localStorage[Counter];
var d =localStorage[Value+i];
i = i + 1; // i becomes 01
var f = localStorage[Value+i];


The i's value is 01 not 1 ... Is there a way to increment the i's value correctly?


More From » html

 Answers
5

LocalStorage can only store string values. You can use parseInt which converts a string into an integer:



var new_value =  parseInt(localStorage.getItem('num')) + 1


You can also use libraries like store.js to do things automatically for you. All you have to do is to include the library:



<script src=store.js></script>


Set a new storage:



var numbers = new Store(numbers)


Put things into it:



numbers.set('num', 2)


Get the value and do anything you want with it:



numbers.get('num') + 1 //output: 3


And you can also go crazy and use some arrays:



numbers.set('nums', [1,2,3])


And change things inside it:



numbers.get('nums')[0] + 3 //output: 4


No type conversion needed. You can also store objects, booleans and other stuff. Just don't forget to save things back in the storage since it doesn't automatically do it.


[#84406] Thursday, July 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mariamiyanab

Total Points: 75
Total Questions: 102
Total Answers: 92

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;