5

Is there a simple way to display my local storage with document.write or document.innerHTML methods? It's being stored on 1 page, trying to display on separate. I'm new to js and just unsure how to build the syntax of those methods with how I'm storing it(if possible).

 $('form').submit(function() {
    var person = $("#FirstName").val() + "." + $('#LastName').val();
 $('input, select, textarea').each(function() {
    var value = $(this).val(),
       name = $(this).attr('name');
       localStorage[person + "." + name] = value;
       window.location.href = "Confirmation.html";
    console.log('stored key: '+name+' stored value: '+value);
});   
});

heres the whole if it helps: http://jsfiddle.net/EUWFN/

2 Answers 2

14

As local storage is Object, you can go trough all it keys and get in values in simple way

for (var key in localStorage) {
  console.log(key + ':' + localStorage[key]);
}

To print it to the screen you can use something like this:

var output = ''; 

for (var key in localStorage) {
  output = output+(key + ':' +localStorage[key])+'\n';
}

$('#DivToPrintOut').html(output);
Sign up to request clarification or add additional context in comments.

5 Comments

does this print the localstorage to the page or just in the console?
@user2912336, you can print localStorage[key] any way you want. For example $('.someclass').html(localStorage['key_name']) or document.write(localStorage['blahblah']).
what if these keys etc are defined on a separate page?
does not matter on what page they was setted. Localstorage is one for all pages within one domain.
do you get eny erorrs is console?
1

If you're using Chrome, press F12 for the Developers Tools.

In the Console type localStorage and press enter.

Or in your js code put console.log(localStorage);.

In the console, click on the little triangle next to line that begins with Storage.

Or in Dev Tools:

  1. click on the Resources tab at the top
  2. click on the little triangle next to Local Storage in the frame on the left

Comments

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.