How to escape quotes in while creating a textbox in jquery / javascript?
<p>I have a couple of span elements and I need to get the contents of these span elements into a textbox so the user can edit.</p>

<p>There might be any number of span elements, so the textboxes have to be created at runtime. The problem I'm having is that when the text contains a single quotes, it's messing with my text box values since it closes one of the quotes.. </p>

<p>This is how I am creating my textbox: </p>

<pre><code>var spanContent = $(value).text();
var tmpTextBox = "&lt;input type='text' id='t" + index + "' value='" + spanContent  + "' class='dynTxt' /&gt;";
</code></pre>

<p>Notice the <strong>value='" + spanContent  + "'</strong> part, this is where it's breaking if <code>spanContent</code> has a single quote in it.. Now, I know I can do this: </p>

<pre><code>value = \"" + spanContent + "\"
</code></pre>

<p>which works fine.. but, when the <code>spanContent</code> has double quotes, it breaks again..</p>

<p>What is the safest way to escape both single quotes and double quotes in this situation, please? I cannot html encode them because textbox values doesn't support them..</p>

<p>I have created a small reproduction of this <a href="http://jsfiddle.net/MTcbX/1/" rel="nofollow">on jsfiddle here</a> for you to play with..</p>

<p>Thanks.</p>
