Q:

innerHTML to ascii

I am attempting to write my own piece of Javascript that converts html to ascii code (for learning purposes) so that the browser will render the code as you would see it in a text editor. 
After looking around on Stack I have gotten as far as below.  I am trying to turn an html element into a string; at this stage I am just trying to .replace() the angular brackets into ascii.  If anyone could tell me where I am going wrong as far as having my test <body> tag showing up in the console that would be much appreciated. 
<code class="lang-html">
  <body></body>
</code>

(function() {
  var html = $('.lang-html').innerHTML;
  html.replace('<', '&lt;');
  html.replace('>', '&gt;');
  console.log(html);
});

Just to clarify, I am expecting that the console would spit out &lt;body&gt;&lt;/body&gt;. 
Any help would be much appreciated. 

A:

If you want get  html code representation of an DOMElement in your browser then you won't need the replace to escape the html special chars.  But you can use the browser to take care of all edge cases. 
You could just use innerHTML/outerHTML and textContent. 
This will e.g. will replace the content of the body with its html code representation.  
var elm = document.getElementsByTagName('body')[0];
elm.textContent = elm.outerHTML;

Or if you just want to have the result as string but not displayed in the browsers then you could wrap that into a function:

function escapeHTML(html) {
  var div = document.createElement('div');
  div.textContent = html;
  return div.innerHTML;
}

console.log( escapeHTML('<div>test</div>') );

