title: javascript function to replace space
body: <p>Hi can you give me a javascript function to replace spaces with <code>&amp;nbsp;</code></p>
  
  <p>i googled and can't get them to work. I'm currently using this function:</p>
  
  <pre><code>  function escapeHTMLEncode(str) 
          {
              var div = document.createElement('div');
              var text = document.createTextNode(str);
              div.appendChild(text);
              return div.innerHTML;
          }
  </code></pre>