Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript decoding html entities [duplicate]

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?

like image 591
petko_stankoski Avatar asked May 23 '12 08:05

petko_stankoski


People also ask

How do you unescape in HTML?

One way to unescape HTML entities is to put our escaped text in a text area. This will unescape the text, so we can return the unescaped text afterward by getting the text from the text area. We have an htmlDecode function that takes an input string as a parameter.

Which method is used to decode the currently encoded HTML code?

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities. If you're wanting to decode instead (the reverse) you can use html_entity_decode().

How do I decrypt HTML code?

Wikipedia has a good expalanation of character encodings and how some characters should be represented in HTML. Load the HTML data to decode from a file, then press the 'Decode' button: Browse: Alternatively, type or paste in the text you want to HTML–decode, then press the 'Decode' button.

How do you decode HTML tags?

HTML character decoding is the opposite process of encoding. The encoded characters are converted back to their original form in the decoding process. It decodes a string that contains HTML numeric character references and returns the decoded string. You can also choose to convert HTML code into JavaScript string.


1 Answers

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 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.

like image 93
Darin Dimitrov Avatar answered Sep 22 '22 07:09

Darin Dimitrov