brunodd brunodd - 1 year ago 145
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

. 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";


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download