tags:

views:

60

answers:

4

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?

+4  A: 

What about

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

?

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

rahul
+1  A: 

Use this

$('#mySpan').html('Hello<br/>World');
piemesons
You were right, it was as simple as that. Many thanks.
lidermin
A: 

Why don't use try this initially on:

$('#mySpan').html('Hello<br />World');
Sarfraz
corrected it guyes !! thanks for spotting that :)
Sarfraz
I wonder what is wrong with above code, why down voting after i have corrected it?????????
Sarfraz
can any one who down votes explain his point please so that i could learn from them probably. thanks
Sarfraz
Sorry Sarfraz, I didn't see your post until I already marked resolved with the previous post, I don't know why you have that -1 ??? I'm new at the forum so I don't know well the rules yet. But thanks for your reply, it was also the right answer.
lidermin
@lidermin: no problem its ok thanks for your answer :)
Sarfraz
A: 

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

kender