brunodd brunodd - 3 months ago 8
Javascript Question

New line in JavaScript strings

I've seen a few questions about how to add a new line in JavaScript strings. Most of them are suggesting to use

\n
. I tried that but the new line is not inserted on my HTML.

Instead I get the following:

<strong>This is the first line
This is the second line</strong>


This is my string:

var myString = "This is the first line \n This is the second line";


Example

Any idea on how I can get a
inserted on my HTML?

Answer

If you want to have the new line in html you need some element like <br />. In fact, each block styled element will do, but <br /> wont add any margin/padding by default, as f. i. a <p> or <h3> would. Anyway, breaking a line html requires any element, since "\n", "\t" and so on are ignored by the browser.

Comments