0

I am building a todo app with localstorage via javascsript,my problem is if i save a note like

First
Second
Third

Then i get in saved not "First Second Third", Is there a way to fix it? This is the javascript function to save the notes

function SaveNotes() {
    var todo = document.getElementById("txtToDo").value;

    var storage = JSON.parse(localStorage.getItem('ToDoList'));
    var arrayLength = storage.length;
  
    storage[arrayLength + 1] = todo;
    localStorage.setItem('ToDoList', JSON.stringify(storage));

    loadNotes();
    clearNote();
}

the whole code and demo you can find here https://jsfiddle.net/r58yzj6v/

Andreas
  • 21,535
  • 7
  • 47
  • 56
user1953051
  • 321
  • 2
  • 7
  • 21
  • Please add a [mcve] _in the question itself_ and not only a link to an external resource. – Andreas Sep 23 '20 at 11:24
  • You could add `white-space: pre-wrap;` to the div where you are displaying the notes. – Reyno Sep 23 '20 at 11:36
  • There is already a jsfiddle link you reproduce problem – user1953051 Sep 23 '20 at 11:43
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask): _"If it is possible to create a live example of the problem that you can link to (for example, on http://sqlfiddle.com/ or http://jsbin.com/) then do so - **but also copy the code into the question itself. Not everyone can access external sites, and the links may break over time.** Use Stack Snippets to make a live demo of inline JavaScript / HTML / CSS."_ – Andreas Sep 23 '20 at 11:48
  • Your problem is _not_ that the line breaks do not get stored and read back properly - they _do_ - but that you output them into a context that does not _display_ line breaks by default. – 04FS Sep 23 '20 at 13:04
  • Does this answer your question? [Render a string in HTML and preserve spaces and linebreaks](https://stackoverflow.com/questions/9492249/render-a-string-in-html-and-preserve-spaces-and-linebreaks) – 04FS Sep 23 '20 at 13:04

1 Answers1

0

You could try using encodeURI() and decodeURI() to convert spaces etc into %nn

let s = `abc
def
ghi`;

let e = encodeURI(s);
console.log(e);
let d = decodeURI(e);
console.log(d);
ATD
  • 1,344
  • 1
  • 4
  • 8