0

I have this piece of code that adds values to an array with an input box. I would like to save that array in localStorage and display the contents of the array on another page.

<button type="button" id="submit">Submit</button>
<button type="button" id="display">Display</button>
window.onload = function() {
    var inputArray = [];
    var input = document.getElementById('inputbox');
    var screen = document.getElementById('screen');

    document.getElementById('submit').onclick = function () {
        inputArray.push(input.value);
        screen.innerHTML = input.value;
    };

    document.getElementById('display').onclick = function () {
        screen.innerHTML = inputArray
    };
} 
4
  • 3
    So what is exactly your problem now ? Could you please elaborate on that? Commented Nov 15, 2016 at 12:52
  • When I refresh the page the contents of the array get lost, I would like to save those contents in localstorage. So that the array doesn't get emptied on a page refresh. Commented Nov 15, 2016 at 12:54
  • Where did you implement logic of adding your array yo LocalStorage? Commented Nov 15, 2016 at 12:57
  • localStorage.setItem("lastname", "Smith"); Commented Nov 15, 2016 at 12:59

3 Answers 3

2

You are not persisting data in localStorage. As localStorage persist only string data, You can stringify while storing and parse while reading.

 window.onload = function() {
     var inputArray = [];

     //Check if data is persisted in localStorge then load in the array
     var str = localStorage.getItem('nyArray')
     if (str) {
         inputArray = JSON.parse(str);
     }

     var input = document.getElementById('inputbox');
     var screen = document.getElementById('screen');

     document.getElementById('submit').onclick = function() {
         inputArray.push(input.value);
         screen.innerHTML = input.value;

         //Store stringifyed string
         localStorage.setItem('nyArray', JSON.stringify(inputArray))
     };

     document.getElementById('display').onclick = function() {
         screen.innerHTML = inputArray
     };
 }
Sign up to request clarification or add additional context in comments.

3 Comments

it should be setItem instead of getItem inside of submit click function.
In your snippet, you spelled localStorage as localStorge
Thank guys for typo.
1

You can stringify your array and then save it in localStorage. When required, read and parse it back to array.

var originalArr = ["V", "A"];
var strArr = JSON.stringify(originalArr);

// to save your array
localStorage.setItem("YOUR_KEY",strArr);

// to read from localstorage
var yourOriginalArray = localStorage.getItem("YOUR_KEY");

Comments

-2

Use Global variable see below

window.savedarray= inputArray

now you can access savedarray in other pages

2 Comments

Downvoted because this is not storing the array in localstorage, but simply storing it as a global variable.
Also storing a global variable (which is bad enough in itself) does not make that variable available in any other page of the site.

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.