Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding html and plain text inside div with jQuery

I'm recieving a string (it may be either plain text or HTML text) from server, and I need to insert it into a div after that.

I can use $div.text(textFromServer) if I know that textFromServer is plain text.

If I know that textFromServer is html I can do something like $(textFromServer).appendTo($div).

The problem is that I don't know for sure whether textFromServer is plain or HTML.

So here's the question: is there a elegant and simple solution for my problem? Or do I have to analyse textFromServer?

like image 324
Eugeny89 Avatar asked Apr 25 '26 12:04

Eugeny89


1 Answers

Simply use html():

$div.html(textFromServer);
like image 78
VisioN Avatar answered Apr 28 '26 00:04

VisioN