New lines in text within a div

domoindal picture domoindal · Jul 11, 2012 · Viewed 42k times · Source

I have a little issue related to when I place a text loaded via ajax call.

I take the contect from a textarea and store it in my database and when I want to show the text in a div, it doesn't respect the new lines, so all the text is continuous.

The following code show a small example:

If you type some text with some new lines, once you click on the button, the text is show into the div and will see that new lines are not show.

How can I solve this?

Answer

Chris.Zou picture Chris.Zou · Jul 14, 2014

I think a better way to achieve this is to add

white-space: pre

or

white-space: pre-wrap

style to your div. see HTML - Newline char in DIV content editable?