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?

2
  • So, you want to do a new line in HTML or in js alert? First can be acheived by <br/>, second by \n Commented Aug 14, 2016 at 11:19
  • @kapilyadav exactly thats my issue! Thank you. Commented Aug 14, 2016 at 11:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I realize this is pedantic, but it is Stackoverflow after all: the <br> 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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.