JavaScript and JQuery - Encoding HTML
<p>I have a web page that has a textarea defined on it like so:</p>

<pre><code>&lt;textarea id="myTextArea" rows="6" cols="75"&gt;&lt;/textarea&gt;
</code></pre>

<p>There is a chance that a user may enter single and double quotes in this field. For instance, I have been testing with the following string:</p>

<pre><code>Just testin' using single and double "quotes". I'm hoping the end of this task is comin'.
</code></pre>

<p>Additionally, the user may enter HTML code, which I would prefer to prevent. Regardless, I am passing the contents of this textarea onto web service. I must encode the contents of the textarea in JavaScript before I can send it on. Currently, I'm trying the following:</p>

<pre><code>var contents $('&lt;div/&gt;').text($("#myTextArea").val()).html();
alert(contents);
</code></pre>

<p>I was expecting contents to display</p>

<pre><code>Just testin&amp;#39; using single and double &amp;#34;quotes&amp;#34;. I&amp;#39;m hoping the end of this task is comin&amp;#39;.
</code></pre>

<p>Instead, the original string is printed out. Beyond just double-and-single quotes, there are a variety of entities to consider. Because of this, I was assuming there would be a way to encode HTML before passing it on. Can someone please tell me how to do this?</p>

<p>Thank you,</p>
