3

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.

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
  • 2
    Use `html` instead of `text`. `$('#description').html(info + '
    ' + footnote);`
    – Tushar Dec 21 '15 at 06:36
  • Not exact duplicate, but can help to understand what the issue is and how it can be solved. Add comments if you think this should not be closed/reopened or find more appropriate dupe. – Tushar Dec 21 '15 at 06:37
  • use `.html()` change `.text()` – guradio Dec 21 '15 at 06:42
  • I've tried to searched before I asked, and found some answers with `.text()` in it, but couldn't quite find the answer to my problem, hence I asked it here. I'm aware that this might already have answers, because I think this is basic, but it's hard to find answers for this particular issue because of so many questions with similar title. – Chen Li Yong Dec 22 '15 at 02:37

1 Answers1

14

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);
Terry
  • 63,248
  • 15
  • 96
  • 118