Solved

how to use Javascript's localStorage.getItem()

Posted on 2014-02-06
4
311 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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
Thanks Cathal! That works! =)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now