As silly as it may sound, I still haven\'t found an appropriate answer.
Let\'s say I want to dynamically create a new DOM element and fill up its textContent/innerTe
I ran into this a while ago. I found a good solution was to use the ASCII representation of carriage returns (CODE 13). JavaScript has a handy feature called String.fromCharCode() which generates the string version of an ASCII code, or multiple codes separate by a comma. In my case, I needed to generate a CSV file from a long string and write it to a text area. I needed to be able to cut the text from the text area and save it into notepad. When I tried to use the method it would not preserve the carriage returns, however, using the fromCharCode method it does retain the returns. See my code below:
h1.innerHTML += "...I would like to insert a carriage return here..." + String.fromCharCode(13);
h1.innerHTML += "Ant the other line here..." + String.fromCharCode(13);
h1.innerHTML += "And so on..." + String.fromCharCode(13);
h1.innerHTML += "This prints hello: " + String.fromCharCode(72,69,76,76,79);
See here for more details on this method: w3Schools-fromCharCode()
See here for ASCII codes: ASCII Codes