0

I'm building a 'to do list' , The user appends new notes to the list after clicking .

I saved all the appended data in the local storage.

Now i want to remove the clicked note from the array and save it back to the local storage. I have this code:

    **//Here i get the note** 
    var getNote = JSON.parse(localStorage.getItem("savedNotes")) || [];
         $("#notes-section").append(getNote);

    **//Here i set the note** 
         getNote.push(note);
      localStorage.setItem("savedNotes", JSON.stringify(getNote)); 



 **//Here i want to remove the note from the array**
    $(document).on('click', '.pin', function() {
$(this).parent().css({opacity: 1.0, visibility: "visible"}).animate({opacity: 0}, 2000);

  for(var i =0 ; i < getNote.length; i++ ){

     getNote.splice(i,1);


       localStorage.setItem("savedNotes", JSON.stringify(getNote)); 
    }

});
Edi Afremov
  • 77
  • 1
  • 2
  • 8

1 Answers1

0

As stated in comments you need to provide only relevant code, but just to clear things out here, the way to go here is:

  • To have an empty array of notes.
  • Add this array to localStorage via localStorage.setItem("savedNotes", JSON.stringify(notes)).
  • Every time you add a note you will need to: parse this array back from localStorage with notes = JSON.parse(localStorage.getItem("savedNotes")).
  • Then push the new note into this array via notes.push(note).
  • Then set this item again with localStorage.setItem("savedNotes", JSON.stringify(notes)), it will update your existing item in the localStorage.

It all relies on Storage.getItem() and Storage.setItem() methods.

And to remove a note from the array you need to do the same thing, expect that you will search for this note in the parsed array and remove it.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Thanks for your answer , But i still can't figure out how to do this ,can you please be more specific? – Edi Afremov Jul 05 '17 at 10:16
  • You need to loop throught the array, find the item to remove an dremove it from the array, you can follow [this](https://stackoverflow.com/q/10024866/3669624). – cнŝdk Jul 06 '17 at 08:07