Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html label inserting newline

I'm trying to set some text to a label dynamically using jQuery. But i can't get the <br> or \n to render and give me new lines. It all shows up on the same line and wraps.

here is my code On JsFiddle

My html:

<label id="myLabel" />

My Javascript:

$("#myLabel").text("This is my first line \n This should be my second line.");
like image 779
capdragon Avatar asked May 06 '11 21:05

capdragon


People also ask

How do I add a line break in labels?

Press Shift+Enter as you type the label text to add a line break. Line breaks are only possible when you use HTML labels, and with word wrapping enabled on shapes. Both are enabled by default.

How do you insert a line break in HTML?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags. Each <br> tag you enter creates another blank line.

Is newline \n or n?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen.

What is the use of \n in HTML?

The \n character matches newline characters.


2 Answers

text() will escape any html code, so instead use html()

$("#myLabel").html("This is my first line <br /> This should be my second line.");
like image 50
ariel Avatar answered Sep 27 '22 22:09

ariel


The problem is that .text() will ignore your html. You should use .html(), soy u can put whatever html you want inside an element. So you'd have:

$('#myLabel').html('This is my first line <br/> This should be my second line.');

Hope this helps. Cheers

like image 21
Edgar Villegas Alvarado Avatar answered Sep 27 '22 23:09

Edgar Villegas Alvarado