0

I have some troubles trying to put information to a input text, the info is from localstorage:

key=client,value:

[{"identification":"123456","firstname":"John","lastname":"White","tel":"123456789"}]

and my question is how can i set the identificacion, firstname, lastname and tel to a inputs type text. Im trying with this jquery

$('#name').val(localStorage.getItem("name"));

but im taking the key, no the values. Thanks for your help!

1
  • log localStorage and let's see.. Commented Aug 13, 2015 at 6:26

4 Answers 4

1

this is your localStorage

{client: "[{"identification":"123456","firstname":"John","lastname":"White","tel":"123456789"}]", se:fkey: "65d75836cdfe6220e7d8fd44a52ef14e,1438917914"}

to save it to the localStorage Object you need to make any object into a string first

you can do it using JSON.stringify(object)

to save it to your localStorage use localStorage.setItem('key', 'value')

and when you want to fetch it on the localStorage

you can do it using localStorage.getItem('key')

if your item is a json object converted into string

you can make use JSON.parse(string) to bring back to be a json object

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the explain :)
@JohanSánchez no problem
0

First get the data from the localStorage and store them in a variable. Then convert your variable from string to valid JSON format. And finally access the needed data. Here is an example :

var client = JSON.parse(storage.getItem(YOUR_CLIENT_KEY));
...
$('#name').val(client[0].firstname);

Comments

0

Local storage (and session storage) are key/value stores where both the keys and values are strings.

So, when pulling data out of localStorage, it will be a string. In your case, you've stored a JSON object in storage, so to use this as an object, you need to get the string out of localStorage and then use JSON.parse to parse the string and convert it to an object, then you can use the object's properties with your jQuery code:

var data = localStorage.getItem("client");
if(data) {   
    var obj = JSON.parse(data);
    $('#name').val(obj[0].firstName + " " + obj[0].lastName);
}

Be aware that JSON.parse() can throw errors if the string is not valid JSON, so you may want to wrap this call in a try/catch block.

Comments

0

You should log localStorage first to see what is stored in it.

It's weird if the value is

 [{"identification":"123456","firstname":"John","lastname":"White","tel":"123456789"}]

because only string type can be stored in localStorage. So you may store the array above via:

localStorage.setItem('key', JSON.stringify(array_above));

Do:

var data = JSON.parse(localStorage.getItem('key'));

when you want to retrieve it.

What's more, your code:

$('#name').val(localStorage.getItem('name'));

works fine.

Here is my jsfiddle demo.

1 Comment

Thanks for this demo bro, appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.