-1

I am trying to retrieve the array from the local storage and display it in innerHTMl. But every time I do a refresh there is nothing in my innerHTML. Although the array is visible in the browser LocalStorage.

document.getElementById("submit").addEventListener("click", arrpush);
var arr = JSON.parse(localStorage.getItem(arr)) || [];

function arrpush(){
    arr.push(document.getElementById('text').value);
    document.getElementById("items").innerHTML = ""; 
    localStorage.arr = JSON.stringify(arr);
    display();
}
function display(){
    document.getElementById("items").innerHTML = "";
    for(i=0;i<arr.length;i++){
    document.getElementById("items").innerHTML += `<li>${arr[i]}</li>`;   
    }   
}
Rajarshi Ghoshal
  • 119
  • 1
  • 12

1 Answers1

0

In this line you're trying to get arr not "arr". "arr" is the name of your localstorage element. arr is your array for the value of "arr".

var arr = JSON.parse(localStorage.getItem(arr)) || []; //getItem("arr")

Further you'll have to call your display function after load

window.onload = function(){
  display();
}
alexP
  • 3,672
  • 7
  • 27
  • 36