I have a Javascript application where I need to escape characters like "<" and ">" in a string.
This looks like an ideal solution:
function escapeHTML(str){ var p = document.createElement("p"); p.appendChild(document.createTextNode(str)); return p.innerHTML; }
or a short alternative using the Option() constructor
function escapeHTML(str){ return new Option(str).innerHTML; }
Q: Does this actually add "p" (and the associated text) to my DOM?
Q: Do I need a "removeChild()" or any other "cleanup" if all I want to save is the escaped string?