user1182504
user1182504

Reputation:

\n new line character won't work

I tried making a web page by writing

document.write("This is the first line \n And this is the second");

The desired effect is:

This is the first line
And this is the second

But I get this:

This is the first line And this is the second

I tried writing <br> inside and it worked. But Why writing an escape \n didn't? Anything I am doing wrong? I am using Firefox.

Upvotes: 2

Views: 313

Answers (3)

user2003007
user2003007

Reputation:

You are not doing anything wrong. Its just the \n character is valid in text related elements like, alert() and <textarea>.

The document.write() command writes to the body of the site or the display (in the HTML). Substitute <br> in place of the \n here since you are writing HTML in the page

Upvotes: 5

Antony
Antony

Reputation: 15106

You use the <pre> tags for things like \n, and <br/> for ordinary HTML.

http://jsfiddle.net/FxpcA/

document.write("<pre>This is the first line \n And this is the second</pre>");

Upvotes: 0

gefei
gefei

Reputation: 19776

instead of \n you have to use <br/>, so that the browser understands it

document.write("This is the first line <br/> And this is the second");

Upvotes: 4

Related Questions