Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to use Javascript's localStorage.getItem()

Posted on 2014-02-06
4
Medium Priority
?
329 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 2000 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
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

688 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