Decode HTML entities with jQuery

While working with the New York Times article API I discovered that they return encoded article leads. This fixed that right up.

var decoded = $('<textarea/>').html(encoded).val();
This entry was posted in Code Clips, JavaScript. Bookmark the permalink.

9 Responses to Decode HTML entities with jQuery

  1. Chad says:

    Perfect.

  2. Neo says:

    Great!

    Similar to PHP htmlentitydecode, but easier…

    So easy can be the life of a JS coder with jQuery ;-)

  3. thanhnv says:

    thanks you,
    it is much helpful for me

  4. mschmitt says:

    Great :p

  5. banksy says:

    Great! Thanks so much! ^^

  6. Gabriel says:

    Anyone know if this still works? Doesn’t seem to be for me.

  7. Mike says:

    Yes. Try this:

    var encoded = “&quot; &apos; &amp; &lt; &gt;”;
    alert(encoded);
    var decoded = $(‘<textarea />’).html(encoded).val();
    alert(decoded);

  8. Manny says:

    Gabriel:
    Only works for . But…
    For first create (instead of the textarea)
    Then code $(‘myinput’).val( $(‘#tmp’).html());
    It will put the decoded text into the element’s value property.
    This is because JQs “html()” function uses the the DOMs “innerHTML” property, then decodes that text. doesn’t have an innerHTML prop.

  9. Phil says:

    What if I need it the other way around?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>