Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to add <br> to a div text using jQuery, but it kept showed <br> instead of newline. How can I do that? [duplicate]

Tags:

jquery

The code is like this:

$('#description').text(info + '<br>' + footnote);

But instead of:

Welcome to the website!
Copyright (c) 2015

It shows:

Welcome to the website!<br>Copyright (c) 2015

If I change '<br>' to '\n', then it displayed as nothing (white space)

Welcome to the website! Copyright (c) 2015

How can I add newline to a div using jQuery? Thanks.

like image 944
Chen Li Yong Avatar asked Nov 24 '25 16:11

Chen Li Yong


1 Answers

That is because you are using .text(), which will escape any HTML tags in it (emphasis my own):

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML.

Use .html() instead:

$('#description').html(info + '<br>' + footnote);
like image 129
Terry Avatar answered Nov 28 '25 04:11

Terry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!