1

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 Answers 4

4

What about

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

?

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

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

Comments

1

Use this

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

Comments

0

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

Comments

0

Why don't use try this initially on:

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

3 Comments

I wonder what is wrong with above code, why down voting after i have corrected it?????????
can any one who down votes explain his point please so that i could learn from them probably. thanks
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.