Unescape HTML in JS

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

Usage

htmlDecode("<img src='myimage.jpg'>"); 
// returns "<img src='myimage.jpg'>"

Comments

  1. User Avatar
    mutter
    Permalink to comment#

    Nice. How about the other way around?

  2. User Avatar
    kayan
    Permalink to comment#

    This reverses it:

    
      function htmlEncode( input ) {
          return String(input)
              .replace(/&/g, '&')
              .replace(/"/g, '"')
              .replace(/'/g, ''')
              .replace(//g, '>');
    }
    
    • User Avatar
      Taufik Nurrohman
      Permalink to comment#

      Maybe this is what you mean:

      function htmlEncode( input ) {
          return String(input)
              .replace(/&/g, '&amp;')
              .replace(/"/g, '&quot;')
              .replace(/'/g, ''')
              .replace(/</g, '&lt;')
              .replace(/>/g, '&gt;');
      }
  3. User Avatar
    Akshay
    Permalink to comment#

    Nice. How about the other way around?

  4. User Avatar
    Eric
    Permalink to comment#

    But, wouldn’t this execute script tags in the html we are trying to unescape? We wouldn’t want that…

  5. User Avatar
    Hayden Schiff
    Permalink to comment#

    This code doesn’t work if you have a tree of nodes to decode instead of just a leaf node. Here’s the solution I used instead:

    function htmlDecode(input) {
        return String(input)
            .replace(/&/g, '&')
            .replace(/"/g, '"')
            .replace(/</g, '');
    }
    
  6. User Avatar
    Jaimin Patel
    Permalink to comment#

    I think this wont work in IE.

  7. User Avatar
    Andrew
    Permalink to comment#

    Is there a way to do this without there being a document?

  8. User Avatar
    Humpty Dumbty
    Permalink to comment#

    How can i escape a chickenburger?
    Are there any solutions?
    Please help?

  9. User Avatar
    AProgrammer
    Permalink to comment#

    Thanks for your post. Exactly what I needed. In case anyone’s interested there’s a similar implementation in the form of an extension method over at https://paulschreiber.com/blog/2008/09/20/javascript-how-to-unescape-html-entities/

  10. User Avatar
    AProgrammer
    Permalink to comment#

    Read through the below stack overflow answer from lucascaro, apparently this is subject to XSS attacks, which he has a resolution for.

    http://stackoverflow.com/questions/1147359/how-to-decode-html-entities-using-jquery

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag