0

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?

Haradzieniec
  • 8,592
  • 29
  • 109
  • 204

2 Answers2

3

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;"
Community
  • 1
  • 1
migueldiab
  • 82
  • 1
  • 4
2

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();
Alex Gidan
  • 2,471
  • 15
  • 27