Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created July 5, 2023 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/721e46b6090895b860241af01561cfb3 to your computer and use it in GitHub Desktop.
Save cferdinandi/721e46b6090895b860241af01561cfb3 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>decodeHTML()</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script>
/**
* Decode HTML entities from an encoded string
* https://stackoverflow.com/a/7394787/1293256
* @param {String} html The encoded HTML string
* @return {String} A decoded HTML string
*/
function decodeHTML (html) {
let txt = document.createElement('textarea');
txt.innerHTML = html;
return txt.value;
}
// Returns "<p>In this course, you'll learn:</p>"
let decoded = decodeHTML('&lt;p&gt;In this course, you&amp;rsquo;ll learn:&lt;/p&gt;');
console.log(decoded);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment