Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  192] [ 6]  / answers: 1 / hits: 132528  / 10 Years ago, mon, may 19, 2014, 12:00:00

I have been working on a project that allows the user to submit memories about a place they have visited and tracks the location of when the memory was submitted. My only problem is trying to use localStorage with the app, I read about the JSON.stringify and JSON.parse, and don't understand how to use them in my code yet.



This is my form.js
It processes the form and grabs the text fields. It clears the form when the add button(on the display details page) or the enter details button is clicked. Finally it receives the information and sends out the message back to the window.



function processForm(){

var locate = document.myform.locate.value;
var details = document.myform.details.value;
var storeData = [];
localStorage.setItem(locate, JSON.stringify(locate));
localStorage.setItem(details, JSON.stringify(details));
alert(Saved: + localStorage.getItem(locate) + , and + localStorage.getItem(details));

var date = new Date,
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear(),
hour = date.getHours(),
minute = date.getMinutes(),
ampm = hour > 12 ? PM : AM;
hour = hour % 12;
hour = hour ? hour : 12; // zero = 12
minute = minute > 9 ? minute : 0 + minute;
hour = hour > 9 ? hour : 0 + hour;

date = month + / + day + / + year + + hour + : + minute + + ampm;

localStorage.setItem(date, JSON.stringify(date));

storeData.push(locate, details, date);
localStorage.setItem(storeData, JSON.stringify(storeData));
}

function clearForm(){
$('#myform').get(0).reset();
}

function retrieveFormInfo(){

var data = JSON.parse(localStorage.getItem(storeData));

var locate = JSON.parse(localStorage.getItem(locate));
$(#locate2).html(Place: + locate);

var details = JSON.parse(localStorage.getItem(details));
$(#details2).html(Description: + details);

var date = JSON.parse(localStorage.getItem(date));
$(#date).html(date);

}


But the major problem I am running into is I do know how to take that information in correctly using the JSON.stringify and JSON.parse and appending it to the window with html elements dynamically, Mainly like a list of memories.



Any help is appreciated!


More From » jquery

 Answers
1

Vanilla JS:


var printStorageBody = function () {
var body = document.querySelector("body");
var pre = document.createElement("pre");
body.innerHTML = "";
pre.innerText = JSON.stringify(localStorage, null, 't');
body.appendChild(pre);
}

jQuery:


var printStorageBody = function () {
$("body").html("");
$("<pre>")
.text(JSON.stringify(localStorage, null, 't'))
.appendTo("body");
}

[#70951] Friday, May 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonathoncamrynv

Total Points: 339
Total Questions: 98
Total Answers: 98

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
;