Local Storage Noob

I'm really new to local storage and really confused on how it works. I am trying to have it save user inputs for future use, is there a way to do that with out overwriting the last stored input?
14 Replies
Jochem
Jochem3y ago
store an array in json format, then when you have to store another value, retrieve the json, turn it back into an array, add the element, then back to json and to localstorage
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Jochem
Jochem3y ago
if you google "store array in localstorage" you should get a ton of examples
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Ævie
ÆvieOP3y ago
I've looked up so much stuff on this and nothing works, do you think you could find something to help me or write something that shows this? My inputs just get overwritten every time.
Jochem
Jochem3y ago
share your code, and I'll try to have a look later
Ævie
ÆvieOP3y ago
Jochem
Jochem3y ago
that's not an array, that's an object if you share your code in a code block instead of a screenshot, it's much easier to edit and show you changes if you use a codepen demonstrating the issue, it's even easier 🙂
Ævie
ÆvieOP3y ago
what is a code block?
Jochem
Jochem3y ago
if you read the "How To Ask A Good Question" thread pinned to the #front-end channel, it'll explain in short, you put ```js on one line, paste your code underneath on a new line, and then on a new line at the bottom put ```, and it adds code highlighting and lets people copy/paste and reshare your code if they change anything to fix it
Ævie
ÆvieOP3y ago
const descriptionValue = document.querySelector('.description-input').value

if(descriptionValue)
{
const entryArray =
{
Descriptions: descriptionValue
};
localStorage.setItem('descriptions', JSON.stringify(entryArray));
console.log(entryArray)
}
const descriptionValue = document.querySelector('.description-input').value

if(descriptionValue)
{
const entryArray =
{
Descriptions: descriptionValue
};
localStorage.setItem('descriptions', JSON.stringify(entryArray));
console.log(entryArray)
}
okay
Jochem
Jochem3y ago
const descriptionValue = document.querySelector('.description-input').value

if(descriptionValue)
{
const currentEntries = localStorage.getItem('descriptions') ?? '[]';

let entryArray;

if (currentEntries !== '') {
entryArray = JSON.parse(currentEntries);
} else {
entryArray = [];
}

entryArray.push(descriptionValue);

localStorage.setItem('descriptions', JSON.stringify(entryArray));
console.log(entryArray);
}
const descriptionValue = document.querySelector('.description-input').value

if(descriptionValue)
{
const currentEntries = localStorage.getItem('descriptions') ?? '[]';

let entryArray;

if (currentEntries !== '') {
entryArray = JSON.parse(currentEntries);
} else {
entryArray = [];
}

entryArray.push(descriptionValue);

localStorage.setItem('descriptions', JSON.stringify(entryArray));
console.log(entryArray);
}
that should work like I said earlier, you have to fetch the old value, parse it, then add to it and save it again
Ævie
ÆvieOP3y ago
it works ❤️ thank you so much this was really frustrating me
Jochem
Jochem3y ago
glad to help 🙂
Want results from more Discord servers?
Add your server