To escape HTML tags as HTML entities with JavaScript, we get the innerHTML
property.
For instance, we write
const escape = document.createElement("textarea");
const escapeHTML = (html) => {
escape.textContent = html;
return escape.innerHTML;
};
const unescapeHTML = (html) => {
escape.innerHTML = html;
return escape.textContent;
};
to define the escapeHTML
and unescpaeHTML
functions.
In it, we set the textContent
property of the text area to the html
string.
And then we return the escape text by returning the innerHTML
property of the element.
To unescape the text, we set the innerHTML
property to the html
string.
And then we get the unescaped text from the textContent
property.