Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing a carriage return in textarea

Tags:

html

textarea

I would like to start a textarea with inside a text that starts some line under the first line. Doing something like:

var myText = '\r \r \r HELLO';

doesn't work: HELLO is written on the first line, while

var myText = 'HELLO \r \r \r HELLO2';

puts correctly HELLO2 after HELLO. This means that \r is correct, but it doesn't work at the beginning of the textarea.

Any suggestions?

like image 394
Masiar Avatar asked Feb 22 '11 18:02

Masiar


People also ask

How do I put carriage return in textarea?

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.

How do I preserve line breaks in textarea?

To preserve line breaks when getting text from a textarea with JavaScript, we can replace whitespace characters with '<br>\n' .

How save user entered line breaks from textarea to database?

Just put the output text between <pre></pre> tags, that will preserve the line breaks.


2 Answers

Don't use \r, use \n

Example: var myText = '\n \n \n HELLO';

like image 84
selbie Avatar answered Sep 28 '22 00:09

selbie


Try inserting &#13; into the text area.

like image 30
Rick Presley Avatar answered Sep 28 '22 00:09

Rick Presley