Solved

how to use Javascript's localStorage.getItem()

Posted on 2014-02-06
4
314 Views
Last Modified: 2014-02-06
Hi,

I'm wondering how I'm supposed to use the localStorage. I have this bit of code:

var car = {};
    car.wheels = 4;
    car.doors = 2;
    car.sound = 'vroom';
    car.name = 'Lightning McQueen';
    console.log( car );
    localStorage.setItem( 'car', car );
    console.log( localStorage.getItem( 'car.doors' ) );

    var oCars = localStorage.getItem( 'car' );
    $.each(oCars, function(key, value) {
        var sCurVal = oCars[key];
    });

Open in new window


The console.log doesn't show anything. This:
console.log( car );

just shows [Object object]

This:
    console.log( localStorage.getItem( 'car.doors' ) );

comes out null

The .each doesn't step through.

What am I missing?

Thank you!
0
Comment
Question by:Victor Kimura
  • 2
  • 2
4 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 500 total points
ID: 39839859
You can't store an array in local storage.
You can stringify the array for storage
Then parse it back when you need it.
Or just store the items separately.
0
 

Author Comment

by:Victor Kimura
ID: 39839922
@Cathal,

 I got that code from some site. Forgot where though

can you give me an example? =)

thanks!
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39839970
Try this, seems to work fine., I'm just alerting the values,
<script>
var car = {};
    car.wheels = 4;
    car.doors = 2;
    car.sound = 'vroom';
    car.name = 'Lightning McQueen';

localStorage.setItem( 'car', JSON.stringify(car) );

oCars = JSON.parse(localStorage['car']);

    $.each(oCars, function(key, value) {
        alert(oCars[key])
    });
</script>

Open in new window

0
 

Author Closing Comment

by:Victor Kimura
ID: 39840375
Thanks Cathal! That works! =)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iframe - style elements within an iframe 7 36
JQuery Autocomplete Tag AJAX (Need nice script) 11 35
Javascript 2 28
How to make google charts responsive 4 8
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question