Solved

how to use Javascript's localStorage.getItem()

Posted on 2014-02-06
4
323 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

734 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