title: Escape single quote in JavaScript
body: <p>I wrote a function as:</p>
  
  <pre><code>function makeTitleEditable($titleContainer){
          var defaultValue = $titleContainer.text().replace("'","\\'");
          $titleContainer.replaceWith("&lt;input id='poll_title' name='poll[title]' value='" + defaultValue +"' type='text'&gt;");
        }
  </code></pre>
  
  <p>Now the problem was I still can't escape the single quote. For example, if </p>
  
  <pre><code>$titleContainer.text() =&gt; I'm lucky
  
  console.log("&lt;input id='poll_title' name='poll[title]' value='" + defaultValue +"' type='text'&gt;") =&gt; &lt;input id='poll_title' name='poll[title]' value='I\'m lucky!' type='text'&gt; 
  </code></pre>
  
  <p>which would generate DOM with value "I" rather than "I'm lucky". How can I solve this problem?</p>