How do i unescape HTML Entities in JS? (change &lt; to <) [duplicate]

How do i unescape HTML Entities in JS?

When googling i literally saw answers with a huge switch and people rolling their own.

I'd like the string &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt; to become <html xmlns="http://www.w3.org/1999/xhtml" >

Answers:

Answer

Create a div, set it's innerHTML and then read innerText

var d = document.createElement("div");
d.innerHTML = "&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;";
alert(d.innerText || d.text || d.textContent);
Answer

Here is a JS implementation of a PHP function that decodes HTML entities

http://phpjs.org/functions/html_entity_decode

Note that this function also depends on:

http://phpjs.org/functions/get_html_translation_table

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.