JavaScript: How to decode an encode HTML-entities

Vit Tertiumnon
Nov 22, 2018 · 1 min read

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();
}

Vit Tertiumnon

Written by

Software Designer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade