JavaScript: How to decode an encode HTML-entities

JavaScript has no methods to encode and decode HTML-entities so you can use these functions.

Decode HTML-entities

function decodeHTMLEntities(text) {
var textArea = document.createElement('textarea');
textArea.innerHTML = text;
return textArea.value;
}

Decode HTML-entities (JQuery)

function decodeHTMLEntities(text) {
return $("<textarea/>")
.html(text)
.text();
}

Encode HTML-entities

function encodeHTMLEntities(text) {
var textArea = document.createElement('textarea');
textArea.innerText = text;
return textArea.innerHTML;
}

Encode HTML-entities (JQuery)

function encodeHTMLEntities(text) {
return $("<textarea/>")
.text(text)
.html();
}

Written by

Software Designer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store