0

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?

brunodd
  • 2,474
  • 10
  • 43
  • 66

1 Answers1

4

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.

philipp
  • 15,947
  • 15
  • 61
  • 106
  • I realize this is pedantic, but it is Stackoverflow after all: the `
    ` tag in HTML does not need a self-closing `/`, though it doesn't hurt anything (because HTML parsers *always* accept but ignore the XML self-closing tag syntax).
    – Pointy Aug 14 '16 at 11:38