1

It might be a silly question but if I have the following html code:

<textarea>This is /n not working</textarea>

It will not display the new line and instead it will output the new line character(/n).

But if I set the value of the textarea using javascript it will work

$('textarea').val("This is \n working");

So I don't understand how the \n character is actually working ?

Here is some fiddle with the above examples: http://jsfiddle.net/mfLt7/106/

Thanks,

Catalin
  • 752
  • 1
  • 16
  • 32

2 Answers2

7

Here is your answer....

use special character for new line in HTML

<textarea id="1">This is &#13;my text</textarea>

enjoy :D

Parth Jasani
  • 2,349
  • 1
  • 19
  • 26
2

In HTML, /n is not a new line, for that matter, nor is \n.

A new line is a new line (except in places where whitespace is collapsed, which is most of HTML, but textareas are one of the exceptions).

<textarea>This is 
 not working</textarea>

In JavaScript, \n is a new line, so JavaScript converts the escape sequence to a new line and then sets the value of the textarea using an actual new line.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Another similar fact: the space in ` ` cause there to be a space between the 2 `span`s – Mark Perera Aug 09 '16 at 10:17
  • One more question: you said that javascript converts the \n character to an actual new line. The question is: what si an actual new line in html ? How the \n character gets translated? – Catalin Aug 09 '16 at 11:12
  • "what si an actual new line in html ?" — It's what you get when you press the enter key on your keyboard. – Quentin Aug 09 '16 at 11:28
  • Ok but, does it have an ascii code or something ? I mean how the actual new line is represented in computer language? It is this code ? So javascript translates \n to ? In this case if I manually enter " " in textarea why it;s not working ? – Catalin Aug 09 '16 at 11:30
  • In what computer language? In HTML it is represented by the literal new line character. You don't need an escape sequence. JavaScript doesn't convert it to an escape sequence. You just use a new line, which you get by pressing the enter key on your keyboard. – Quentin Aug 09 '16 at 11:33
  • So when you hit "Enter" button while typing in textbox, "enter" is actually translated into "/n" - new line. But if you type in the textbox "/n" it is not going to render a new line because \n it's escaped.. So if you enter "/n" it gets translated into "//n". Thanks ! – Catalin Aug 09 '16 at 13:54
  • No. If you type a new line between the start and end tag of a textarea in HTML, then you get a new line in it. If you type a new line in the rendered textarea in the browser, then you get a new line in it. Nothing will convert it to `\n` (you need to get your slashes the right way round!) as nothing which uses that escape sequence is involved. Even if you read the value in (for example) JavaScript, it is still a new line because `\n` is how you represent a new line in JavaScript **source code**, internally it is just a new line. – Quentin Aug 09 '16 at 13:57
  • (and if you were to write JavaScript with `foo = "\n"` then the escape sequence would be converted to a new line when the JavaScript parser had parsed the source code). – Quentin Aug 09 '16 at 13:58