Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my JS code,
JavaScript
var s1 = 'the 1st line';
var s2 = 'the 2nd line';
var s = s1 + '\n' + s2;   

The result is: the 1st linethe 2nd line.
Why the '\n' does not work? Thanks.

What I have tried:

Have a new line: Not working using \n
Posted
Updated 4-Mar-16 6:00am
Comments
Thomas Daniels 4-Mar-16 11:39am    
What do you mean by "not working"? Where are you trying to display the result string?
s yu 4-Mar-16 14:31pm    
See the discussion below - the app is for an outlook email. Thanks.

Because this is the web you would use
HTML
< br />
instead of \n.
 
Share this answer
 
v2
Comments
s yu 4-Mar-16 11:42am    
Please re-post your answer since a part of it looks to be filtered out. Thanks.
ZurdoDev 4-Mar-16 11:44am    
I can see it. It's the br tag in html.
s yu 4-Mar-16 12:10pm    
Ryan: Thanks for your response. Actually, it is for an email's body rather than a web page. In .Net I can use System.Environment.NewLine. But I don't know how in JS. Any additional hint? Thanks.
Afzaal Ahmad Zeeshan 4-Mar-16 12:51pm    
In email, <br> element would work perfectly. Most of the email clients would use the email body as HTML and then render it using an HTML engine.

Gmail does that. So, there won't be a trouble. Also, the new line character is old school, C, C++, Java and C# (maybe a few more) and are used in streams of string. Not in rendering.

Can you use that character in HTML? In XAML? No! Because markups don't understand the basic strings, they have their own syntax.
s yu 4-Mar-16 13:29pm    
Tested w/ <br/> like that: var s = '1st line <br/> 2nd line'. When loaded into Outlook, it shows 1st line <br/> 2nd line.
Mt email body is pure text format. Thanks.
If the result goes to a text file, use \n for new line.
If the result goes to a HTML file, use <br/> for new line.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900