<textarea cols='60' rows='8'>This is my statement one.\n This is my statement2</textarea>
<textarea cols='60' rows='8'>This is my statement one.<br/> This is my statement2</textarea>
I tried both but new line is not reflecting while rendering the html file. How can I do that?
I think you are confusing the syntax of different languages.
is (the HtmlEncoded value of ASCII 10 or) the linefeed character literal in a HTML string. But the line feed character does NOT render as a line break in HTML (see notes at bottom).\n
is the linefeed character literal (ASCII 10) in a Javascript string.<br/>
is a line break in HTML. Many other elements, eg<p>
,<div>
, etc also render line breaks unless overridden with some styles.Hopefully the following illustration will make it clearer:
A few points to note about Html:
innerHTML
value of theTEXTAREA
element does not render Html. Try the following:<textarea>A <a href='x'>link</a>.</textarea>
to see.P
element renders all contiguous white spaces (including new lines) as one space.TEXTAREA
renders LF as a new line inside the text area box.I've found
String.fromCharCode(13, 10)
helpful when using view engines. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCodeThis creates a string with the actual newline characters in it and so forces the view engine to output a newline rather than an escaped version. Eg: Using NodeJS EJS view engine - This is a simple example in which any \n should be replaced:
viewHelper.js
EJS
Renders
replaceAll:
You might want to use
\n
instead of/n
.Fiddle showing that it works: http://jsfiddle.net/trott/5vu28/.
If you really want this to be on a single line in the source file, you could insert the HTML character references for a line feed and a carriage return as shown in the answer from @Bakudan:
After lots of tests, following code works for me in Typescreipt
T.innerText = "Position of LF: " + t.value.indexOf("\n");