Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Display line breaks within textarea

I have asked this question now several ways, but still have not gotten an answer.

When you capture asci text in a textarea that includes line breaks, it seems to insert invisible characters in the form of line breaks when you hit return. The line break seems to have the format \r for return or \n for new line. These characters are not visible in the text but are there somewhere.

However, when I put this code in a textarea, I cannot get it to display a linebreak. In fact, I cannot find any code that placed between textarea tags displays a line break.

Can someone show a way to display line breaks in a textarea, ie.

<textarea>first line <some code> second line </textarea>

I have really tried about everything you could possibly imagine. For example, if I try <textarea> first line "\r\n" second line</textarea> it just displays

`firstline "\r\n"` second line

Note: anything involving <br> will not solve this problem as this is not about html, but rather plain text displaying in textarea box.

Thanks for suggested code to display line breaks in textarea.

like image 968
user1260310 Avatar asked Nov 21 '12 01:11

user1260310


People also ask

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 do you keep a line break in HTML?

The <pre> tag defines preformatted text. 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.

How do I insert a line break in a text box?

Of course, there are situations in which there is need to add line breaks. You can do so by simply hitting Shift + Return or Shift + Enter , respectively.

Can we use DIV inside textarea?

No, it is not possible(it will not render the UI).


2 Answers

Well if you're doing this through pure HTML, then you can do it one of two ways. Just add lines by pressing enter or using the ASCII characters &#013; &#010;

jsFiddle

like image 175
Jeremy Morehouse Avatar answered Oct 12 '22 22:10

Jeremy Morehouse


If you are using javascript you can use the textarea element value Property.

var tb = document.getElementById("tb");

var newLine = "\r\n";

var text = "Output: " + newLine + "-------------------" + 
                        newLine + "line 1:	" + 
                        newLine + "line 2:	";

tb.value = text;
//console.log(text);
<textarea id="tb" rows=5>
Output: &#13;&#10;-------------
</textarea>

Run it on JSFiddle

like image 5
stomtech Avatar answered Oct 12 '22 21:10

stomtech