lidermin
lidermin

Reputation: 2832

Write breakline in HTML from JavaScript

I have some JavaScript code that does some stuff, but at some point I need to do this:

$('#mySpan').text('Hello\nWorld');

Here is my HTML code:

<span id="mySpan"></span>

So I can finally get this:

Hello
World

The problem is that HTML is not detecting the breakline inserted from the JavaScript. I also tried this:

$('#mySpan').text('Hello<BR/>World');

But it doesn't work either.

How should I do it?

Upvotes: 1

Views: 192

Answers (4)

rahul
rahul

Reputation: 187110

What about

$('#mySpan').html('Hello<br/>World');

?

text only sets the text contents whereas html sets the HTML contents.

Upvotes: 4

Sarfraz
Sarfraz

Reputation: 382909

Why don't use try this initially on:

$('#mySpan').html('Hello<br />World');

Upvotes: 0

kender
kender

Reputation: 87271

Maybe use .append() instead of .text(), if should break the line with your <br /> properly.

Upvotes: 0

Mohit Jain
Mohit Jain

Reputation: 43959

Use this

$('#mySpan').html('Hello<br/>World');

Upvotes: 1

Related Questions