Skip to content Skip to sidebar Skip to footer

Localstorage Troubles

I am trying to create a webapp, which you will click in a button, and his ID is saved to the localstorage, and change the style. But when I save in localstorage, the last button th

Solution 1:

localStorage is a key-value store. By setting a key, you replace the value of that key if it is already set. However, you could store a stringified array in localStorage, then retrieve it and add to it when needed.

//Store an array in local storage
localStorage.setItem('arr',JSON.stringify([6,8,9])

//retrieve the array, add to it, then save it again
var arr = JSON.parse(localStorage.getItem('arr'));
arr.push(23)
localStorage.setItem('arr', arr)

Post a Comment for "Localstorage Troubles"