Solved

how to use Javascript's localStorage.getItem()

Posted on 2014-02-06
4
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

630 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