Issue
I am trying to retrieve the data stored in local storage but I get the value as [object Object]
. I know that local storage only takes strings and the data has to be JSON.stringify
when setting and JSON.parse
when getting but I still get it as [object Object]
.
Instead of [object Object]
I want to display the value entered in the input field
I am trying to get a list of items on button click. To do this I am saving the value entered in the input field and pushing it to an array.
I have created an empty array like
var todosArr = Array();
when a value is entered in the input field and the submit button is clicked, this is the code executed
todosArr.push({"listname":todoTxtField.value});
localStorage.setItem('todos', JSON.stringify(todosArr));
and to display the listing in the span this is the code written
listSpan.innerText = JSON.parse(localStorage.getItem('todos'));
Solution
For displaying first element in array:
var arr = JSON.parse(localStorage.getItem('todos'))
listSpan.innerText = arr[0].listname;
or for all elements in array:
arr.forEach(o => {
listSpan.innerText += o.listname
})
Answered By – Nikola Pavicevic
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0