Haradzieniec
Haradzieniec

Reputation: 9340

How to convert string to HTML friendly string

We have $str = " hello";

What's the best way to get $newstr equal to  hello; and so on to be sure it is displayed on the page the same way using jQuery?

Upvotes: 0

Views: 182

Answers (2)

migueldiab
migueldiab

Reputation: 80

I think both answers by Matt Schmiermund and Alex Gidan are directing you to the right place.

You should try something like this :

var a = "<div>Your HTML & More</div>";
var span = document.createElement("span");
$(span).text(a)
$(span).html()

this will return the following :

"&lt;div&gt;Your HTML &amp; More&lt;/div&gt;"

Upvotes: 3

Alex Gidan
Alex Gidan

Reputation: 2679

If you have to do it with JQuery (directly from your JS) you can use .text() / .html() functions:

 $('<div/>').text(value).html();


 $('<div/>').html(value).text();

Upvotes: 2

Related Questions