Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve line breaks when pasting into textarea

Tags:

html

textarea

I have a textarea. When I paste text with line breaks into it, the line breaks are automatically removed. But when I manually enter line breaks (hitting carraige return), they are preserved.

How do I force the textarea to preserve line breaks with pasted text?

like image 429
Scott Avatar asked Jul 28 '10 19:07

Scott


People also ask

How do I preserve line breaks in textarea?

Preserve Newlines, Line Breaks, and Whitespace in HTML If you want your text to overflow the parent's boundaries, you should use pre as your CSS whitespace property. Using white-space: pre wraps still preserves newlines and spaces.

How do you store line breaks in database?

Insert SQL carriage return and line feed in a string In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break. Char(13) – Carriage Return.

Does textarea support new line?

To add line breaks to a textarea, use the addition (+) operator and add the \r\n string at the place where you want to add a line break, e.g. 'line one' + '\r\n' + 'line two' . The combination of the \r and \n characters is used as a newline character. Here is the HTML for the examples in this article. Copied!

How do you preserve a new line in HTML?

Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.


1 Answers

Don't know if this applies to all browsers, but in Google Chrome (6 and 7), line breaks in pasted text gets removed if the textarea's style is set to white-space: nowrap;.

I used this style setting to prevent the default wrapping of long lines and experienced the same problem as yours. Later I realized that setting the textarea attribute wrap to off does a much better job. Be aware that wrap="off" is widely supported but not standardized.

like image 141
Oben Sonne Avatar answered Sep 22 '22 19:09

Oben Sonne