0

I need some help with figuring out how local storage and JSON works.

I have the following html:

<!DOCTYPE html>
<html>
<head>
 <link rel="stylesheet" type="text/css" href="style.css"> 
 <script src="Script.js"></script> 
</head>
<body>
 <form name="test" method="post" action="javascript:storage()">
 <input name='surname' id="surname" value='surname'>
 <input name='lastname' id="lastname" value='lastname'>
 <input type="submit" value="test">
 </form>
 <div id="tabletest"></div>
</body>
</html>

the following javascript:

function storage(){
 var surname = document.getElementById('surname').value;
 var lastname = document.getElementById('lastname').value;
 var person = {
 "surname" : surname,
 "lastname" : lastname,
 "dateofbirth" : "01-01-1990" 
 };
 person = JSON.stringify(person);
 localStorage.setItem('person', person);
 var person2 = localStorage.getItem('person');
 var persons = JSON.parse(person);
 var tabletest = document.getElementById('tabletest');
 var person3 = JSON.parse(person2);
 tabletest.innerHTML += JSON.stringify(person3);
}

My problems/troubles: The output I get in tabletest is this:

{ "surname":"surname", "lastname":"lastname", "geboortedatum":"01-01-1990" }
  • How do I get only the surname and the lastname in the 'tabletest' div?
  • How do I add a new value with the inputs from the textfields when the submitbutton is clicked (because push doesn't work)?
Kutyel
9,1744 gold badges35 silver badges64 bronze badges
asked May 4, 2015 at 12:31
3
  • var person3 = JSON.parse(person2); tabletest.innerHTML += person3.surname + " " + person3.lastname Commented May 4, 2015 at 12:35
  • tabletest.innerHTML += person3.surname + person3.lastname; Commented May 4, 2015 at 12:35
  • push doesn't work because person is not array it is an object Commented May 4, 2015 at 12:36

3 Answers 3

0

To get lastname(or I should say any value inside JSON) use

tabletest.innerHTML +="Lastname:"+person3.lastname+" Sirname:"+person3.sirname;

And to add to the JSON use:

person.newName = newName 
//or 
person["newName"] = newName;
answered May 4, 2015 at 12:37
Sign up to request clarification or add additional context in comments.

Comments

0

Localstorage works on key-value pairs. Since you are using the same key, 'person', you are simply overwriting previous value. You could use an array of persons which you store using 'person' key but you are responsible for parsing/stringifying the array each time.

var personsStore = [];
function storage() {
 var s = localStorage.getItem("person");
 if (s) {
 personsStore = JSON.parse(s);
 }
 var person = {...} //after you get properties from dom input
 personsStore.push(person);
 var stringForm = JSON.stringify(personsStore");
 localStorage.setItem("person", stringForm);
 var tabletest = document.getElementById('tabletest');
 tabletest.innerHtml += stringForm;
} 

If you want particular attributes the easiest way to do that is to use a tool like underscore (underscore.org). Appending to 'tabletest' becomes

 tabletest.innerHtml += JSON.stringify(_.map(personStore, fucntion(p) {
 return _.pick(p, "firstname", ....);
 });
answered May 4, 2015 at 12:47

3 Comments

Your code doesn't work.. the JSON.stringify has a " in it.. I removed it but it still doesn't show anything when I click.. I have put my person code into the var person { } .
:-) . Apologies. I quickly typed that and I assumed you'd get my logic therefore I didn't expect the fragment I wrote above to just work; Its just to guide you. The " in JSON.stringify(...) is a typo. Having personsStore external variable is not necessary too.
Did you add the code: tablest.innerHtml += JSON.stringify(... ? If so did you include [link](underscore.org) ? There is a typo on function (p).... If nothing happens then could be a bug. Please check.
0

Here is a fiddle

Right now, your logic shows you grabbing the value of first and last name, which is, currently: surname and lastname, respectively:

<input name='surname' id="surname" value='surname'>
 <input name='lastname' id="lastname" value='lastname'>

You need to run this function on the button click event and get the value and simply use the storage setItem(key,value) function. Here is the documentation . You only need to stringify, then parse on the storage data. After that, it is an object that you can get the properties from.

person = JSON.stringify(person);
 localStorage.setItem('person', person);
 var person2 = localStorage.getItem('person');
 var persons = JSON.parse(person2);
 var tabletest = document.getElementById('tabletest');
 tabletest.innerHTML += persons.surname + ' ' + persons.lastname;
You were very close on the logic, but you needed something like this:
 document.getElementById('btnTest').onclick = storage;

I also modified the 'submit' button to a standard 'button' element so that the form doesn't post:

 <button id='btnTest' value="test">Test</button>

You could then do Ajax. Otherwise, you would need to do a pre-submit function

answered May 4, 2015 at 12:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.