Javascript decoding html entities

JavascriptJquery

Javascript Problem Overview


> Possible Duplicate:
> How to decode HTML entities using jQuery?

I want to convert this text:

"<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>"

to html, with tags and everything in Javascript or Jquery. How to do this?

Javascript Solutions


Solution 1 - Javascript

var text = '<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>';
var decoded = $('<textarea/>').html(text).text();
alert(decoded);

This sets the innerHTML of a new

element (not appended to the page), causing jQuery to decode it into HTML, which is then pulled back out with .text().

Live demo.

Solution 2 - Javascript

There is a jQuery solution in this thread. Try something like this:

var decoded = $("<div/>").html('your string').text();

This sets the innerHTML of a new <div> element (not appended to the page), causing jQuery to decode it into HTML, which is then pulled back out with .text().

Solution 3 - Javascript

Using jQuery the easiest will be:

var text = '&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;';

var output = $("<div />").html(text).text();
console.log(output);

DEMO: http://jsfiddle.net/LKGZx/

Solution 4 - Javascript

I think you are looking for this ?

$('#your_id').html('&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;').text();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpetko_stankoskiView Question on Stackoverflow
Solution 1 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 2 - JavascriptBojanglesView Answer on Stackoverflow
Solution 3 - JavascriptVisioNView Answer on Stackoverflow
Solution 4 - JavascriptKaidulView Answer on Stackoverflow