Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using new line(\n) in string and rendering the same in HTML

I have a string say

string display_txt = "1st line text" +"\n" + "2nd line text"; 

in Jquery, I am trying to use

('#somediv').html(display_txt).css("color", "green") 

quite clearly I am expecting my msg to be displayed in 2 lines, but instead \n is being displayed in the message. Any quick fixes for that?

Thanks,

like image 664
KeenUser Avatar asked Dec 20 '11 10:12

KeenUser


People also ask

How do I add a new line to a string in HTML?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags. Each <br> tag you enter creates another blank line.

What does \n do in HTML?

The \n character matches newline characters.

Can you put \n in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do I print a new line in HTML?

HTML Line Breaks The HTML <br> element defines a line break.


1 Answers

Set your css in the table cell to

white-space:pre-wrap; 

document.body.innerHTML = 'First line\nSecond line\nThird line';
body{ white-space:pre-wrap; }
like image 174
vsync Avatar answered Nov 16 '22 03:11

vsync