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.");
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.
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.
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.
The \n character matches newline characters.
text()
will escape any html code, so instead use html()
$("#myLabel").html("This is my first line <br /> This should be my second line.");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With