SO Answer / why does this work to encode and decode HTML
See: http://stackoverflow.com/questions/11920718/why-does-this-work-to-encode-and-decode-html/
by KooiInc MeHere
HTML
<textarea id="t" cols=80, rows=3></textarea>
JavaScript
function htmlcode(htmlstr,decde){
var x = document.createTextNode()
,y = document.createElement('div');;
x.textContent = htmlstr;
if (decde) { y.innerHTML = htmlstr; }
else { y.appendChild(x); }
return decde ? (y.innerText || x.textContent) : y.innerHTML;
}
var res = document.querySelector('#t');
res.value = 'encoded: '+htmlcode('<p /> + <br /> sdfsdfdsfdsfsdfsdfsdfsfsfsfdsf');
res.value += '\ndecoded the value of this textarea:\n**'+htmlcode(res.value,true);