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'>"
Nice. How about the other way around?
This reverses it:
Maybe this is what you mean:
Nice. How about the other way around?
But, wouldn’t this execute script tags in the html we are trying to unescape? We wouldn’t want that…
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:
Hmm, the comment filter seems to have torn my function to pieces. I’ll just post it to Gist instead: https://gist.github.com/oxguy3/18d92821fe931945c86f
I think this wont work in IE.
Is there a way to do this without there being a document?
How can i escape a chickenburger?
Are there any solutions?
Please help?
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/
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
<hugeLikeThanks>Thank you for this!</hugeLikeThanks>
For anyone finding this in 2020 you should instead use DOMParser API, example:
https://stackoverflow.com/questions/1912501/unescape-html-entities-in-javascript/34064434#34064434