Take the 2-minute tour ×

How do I use jQuery to decode HTML entities in a string?

Or how do I stop jQuery from encoding a string with HTML entities in the first place?

share|improve this question
It would be great if you will try elaborate your question as well extend it with some examples of what you are asking. –  Artem Barger Jul 18 '09 at 12:05
Could you clear up what you mean by "stop jquery from encoding a string with html entities"? In which context is this happening? –  oggy Jul 18 '09 at 12:06
-1 against the grain. Why would you want to use jQuery for that when JavaScript has built in methods? –  Christophe Mar 29 '13 at 17:58
@EddyR please remove tom's answer as accepted. It should be at the bottom not the top! Look at Mike Samuel's comment for why you shouldn't use jQuery.html(). –  Alan Hamlett May 23 '13 at 21:48
add comment

11 Answers

up vote 303 down vote accepted

Actually, try

var decoded = $("<div/>").html(encodedStr).text();
share|improve this answer
thanks a lot! worked for me too –  Purefan Sep 28 '10 at 12:02
Do not do this with untrusted input. Many browsers load images and fire related events even if the node is not attached to the DOM. Try running $("<div/>").html('<img src="http://www.google.com/images/logos/ps_logo2.png" onload=alert(1337)>'). In Firefox or Safari it fires the alert. –  Mike Samuel Mar 16 '11 at 20:37
@ekkis, you need to strip tags before trying to decode entities. str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/g, "") or something similar. –  Mike Samuel May 29 '11 at 5:07
A better implementation (in my opinion) that strips most HTML tags (courtesy of Mike) from the input is in my answer of a similar question. It also does not have the overhead of jQuery so it's quite suitable to other environments. –  Robert K Mar 7 '12 at 21:41
As @MikeSamuel say, $('<div/>') is too dangerous for use. More like $('<textarea/>') - it more safety. –  trijin Feb 20 at 23:08
show 6 more comments

Like Mike Samuel said, don't use jQuery.html().text() to decode html entities as it's unsafe.

Instead try the Underscore.js utility-belt library which comes with escape and unescape methods:



share|improve this answer
This actually deserves way more upvotes! Definitely my preferred solution. They included unescape in the docs by now, btw. –  lethal-guitar May 17 '13 at 13:01
Thanks, I updated my answer with a direct link to the new docs. –  Alan Hamlett May 23 '13 at 19:31
_.unescape("&#39;") results in just "&#39;" instead of a single-quote. Is there something I'm missing or does underscore not escape to HTML entity codes as shown on: w3schools.com/tags/ref_entities.asp –  Jason Axelson Dec 2 '13 at 19:31
@JasonAxelson created github.com/jashkenas/underscore/issues/1370 for this. –  Alan Hamlett Dec 9 '13 at 10:17
The bug on github was closed as "Won't fix"; that means that this solution doesn't work and will not work. –  Igor Chubin Dec 29 '13 at 11:03
add comment

I think you're confusing the text and HTML methods. Look at this example, if you use an element's inner HTML as text, you'll get decoded HTML tags (second button). But if you use them as HTML, you'll get the HTML formatted view (first button).

<div id="myDiv">
    here is a <b>HTML</b> content.
<br />
<input value="Write as HTML" type="button" onclick="javascript:$('#resultDiv').html($('#myDiv').html());" />
<input value="Write as Text" type="button" onclick="javascript:$('#resultDiv').text($('#myDiv').html());" />
<br /><br />
<div id="resultDiv">
    Results here !

First button writes : here is a HTML content.

Second button writes : here is a <B>HTML</B> content.

By the way, you can see a plug-in that I found in jQuery plugin - HTML decode and encode that encodes and decodes HTML strings.

share|improve this answer
try it here: jsfiddle.net/YqQ3s –  cawecoy May 21 '13 at 17:24
add comment

The question is limited by 'with jQuery' but it might help some to know that the jQuery code given in the best answer here does the following underneath...this works with or without jQuery:

function decodeEntities(input) {
  var y = document.createElement('textarea');
  y.innerHTML = input;
  return y.value;
share|improve this answer
add comment
var decoded = $('<textarea/>').html(encoded).val();
share|improve this answer
This didn't work for me. –  Senseful Oct 16 '10 at 20:34
the poster didn't test it. I get a blank –  ekkis May 29 '11 at 1:33
This should not work at all. –  Shuaib Nawaz Sep 16 '11 at 15:37
sorry, the <textarea/> disappeared when copying! thanks @tim-cooper for the edit. –  lucascaro Jan 17 '12 at 19:01
Nice solution, works well! –  stian Jan 24 '13 at 16:38
add comment


myString = myString.replace( /\&amp;/g, '&' );

It is easiest to do it on the server side because apparently JavaScript has no native library for handling entities, nor did I find any near the top of search results for the various frameworks that extend JavaScript.

Search for "JavaScript HTML entities", and you might find a few libraries for just that purpose, but they'll probably all be built around the above logic - replace, entity by entity.

share|improve this answer
add comment

I just had to have an HTML entity charater (⇓) as a value for a HTML button. The HTML code looks good from the beginning in the browser:

<input type="button" value="Embed & Share  &dArr;" id="share_button" />

Now I was adding a toggle that should also display the charater. This is my solution

        $(this).attr("value", "Embed & Share " + $("<div>").html("&uArr;").text());

This displays ⇓ again in the button. I hope this might help someone.

share|improve this answer
add comment

You have to make custom function for html entities:

function htmlEntities(str) {
share|improve this answer
add comment

I think that is the exact opposite of the solution chosen.

var decoded = $("<div/>").text(encodedStr).html();

Try it :)

share|improve this answer
Sorry, this encodes it further! –  Agent47DarkSoul Sep 25 '13 at 6:23
add comment

The easiest way is to set a class selector to your elements an then use following code:

    $('.classSelector').each(function(a, b){

Nothing any more needed!

I had this problem and found this clear solution and it works fine.

share|improve this answer
add comment

To decode HTML Entities with jQuery, just use this function:

function html_entity_decode(txt){
    var randomID = Math.floor((Math.random()*100000)+1);
    $('body').append('<div id="random'+randomID+'"></div>');
    var entity_decoded = $('#random'+randomID).html();
    return entity_decoded;

How to use:


var txtEncoded = "&aacute; &eacute; &iacute; &oacute; &uacute;";


<input id="some-id" type="text" />
share|improve this answer
add comment

Your Answer


