I am storing content in the database, for example:
Hello
This
is
Text
and when I pass that to a textarea, it stays with the new line breaks. But if I pass that text to a div with content editable, it would stay like this:
Hello This is Text
How can I fix this problem?
You could search and replace newlines with
<br />
. http://jsfiddle.net/fPv6S/1/To add some info on @Explosion Pills correct answer and extract some info from MDN:
The CSS
white-space
attribute is there to help:pre:
This might result in unwanted horizontal scrollbars as shown in the example!
pre-wrap:
As @ceremcem pointed out the line breaks at the end of the box will not be transferred when the text is copy-pasted, which makes sense since these line breaks are not part of the text formatting but rather of the visual appearence.
pre-line:
EDIT 08/14/2018:
In Chrome you might experience troubles: Pressing Enter will generate a new
<div>
inside your contenteditable. To prevent that you can either press Shift+Enter or setdisplay: inline
to the contenteditable<div>
as seen in the fiddle.Set a style on the div:
white-space: pre
orwhite-space: pre-wrap
Example: http://jsfiddle.net/fPv6S/
Try this......
That will do it...