0

I store a text in my database as

$dataStorage = "This is my short string.\n\nThank you." When i fetch the string from database and display in input box or text area, It appears just as it is stored in the database (i.e with the new break line" as below

This is a short string. 

Thank you.

Now, i am trying to display the text in div. The text appears but the new lines do not show. So it appears like

This is a short string.Thank you

How can i fix this using jquery

HTML

<div class="chat-content rounded" id="msg">
</div>

JS

//result is the string from the database

$('#msg').val(result);
  • 1
    Does this answer your question? [How do I replace all line breaks in a string with
    elements?](https://stackoverflow.com/questions/784539/how-do-i-replace-all-line-breaks-in-a-string-with-br-elements)
    – Mohammad Jan 29 '21 at 13:20

2 Answers2

1

You can replace the newlines by br:

result.replace(/\n/g,"<br />");
Gert B.
  • 2,282
  • 18
  • 21
0

Are you using PHP to fetch the text string from the database? Then you can apply nl2br($your_text) to the fetched result, which converts the new-line commands to HTML linebreaks (<br/>)

Johannes
  • 64,305
  • 18
  • 73
  • 130