Sign in
Log inSign up

Local storage issue

Deactivated User's photo
Deactivated User
·Jan 25, 2020

Hello, I'm working on a notes taking app with local storage but I think I got stuck on this: github.com/matteobarone/notepadjs/tree/loc… (code below)

import {localStorage} from './local-storage/local-storage';
document.addEventListener('DOMContentLoaded', () => {
const listItemElement = document.querySelector('.notepad__list');
const inputElement = document.querySelector('.notepad__input');
inputElement.addEventListener('keyup', (event) => {
if (event.keyCode === 13) {
addItem(event.target.value, listItemElement);
event.target.value = null;
 }
 })
});
function addItem(text, list, nextNotes) {
const el = document.createElement('li');
const textNode = document.createTextNode(text);
el.appendChild(textNode);
el.classList.add('notepad__item');
list.appendChild(el);
// retrieve the notes that are already in localstorage
const notes = JSON.parse(localStorage.getItem('notes'));
// pushing your new note into the notes array - mutation
notes.push(text);
// setting the notes array JSON into localStorage
localStorage.setItem('notes', JSON.stringify(nextNotes));
}

I am basically trying to retrieve notes from the local storage but all I get is that the previous note is overridden by the next one. I have already looked up online and asked questions on stackoverflow but I don't seem to get to the answer. What am I doing wrong?

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct