HTML Encoder

Created: 4 April 2012  Modified:

I put together a quick little HTML Encoder for myself using JavaScript. I find I am frequently searching for an online encoder when I am writing my posts. The code is below and I find it entertaining that it was encoded for display using the code itself:).

<span class="codeCaption">html-encode</span>
<pre class="prettyprint highlight">
  <code class="language-html" data-lang="html">
  function encode() {
    var theHTML = document.getElementById('rawArea').value;
    theHTML= theHTML
            .replace(/&/g, '&')
            .replace(/"/g, '"')
            .replace(/'/g, ''')
            .replace(/</g, '<')
            .replace(/>/g, '>');

<form id="rawForm">
   <textarea id="rawArea" rows="5" cols="40"></textarea>
   <input value="Encode" type="button" onclick="encode(); return false;"/>

<form id="encodedForm">
   <textarea id="encodedArea" rows="5" cols="40"></textarea>
   Less Is More