title: If a parameter of event handler contains certain characters, JavaScript does not execute properly
body: <p>php:</p>
  
  <pre><code>$str1 = "AAA\r\nBBBB\\CCC";
  echo"&lt;textarea id='aa1' onfocus='erase(\"".$str1."\", \"aa1\");'&gt;&lt;/textarea&gt;";
  $str2 = "AAABBBB\\CCC";
  echo"&lt;textarea id='aa2' onfocus='erase(\"".$str2."\", \"aa2\");'&gt;&lt;/textarea&gt;";
  $str3 = "AAABBBB\CCC";
  echo"&lt;textarea id='aa3' onfocus='erase(\"".$str3."\", \"aa3\");'&gt;&lt;/textarea&gt;";
  $str4 = "AAABBBBCCC";
  echo"&lt;textarea id='aa4' onfocus='erase(\"".$str4."\", \"aa4\");'&gt;&lt;/textarea&gt;";
  </code></pre>
  
  <p>javascript:</p>
  
  <pre><code>function erase(str, id)
  {
      alert("good");
      var field = document.getElementById(id);
      if(field.value == str)
      {
          field.value = '';
      }
  }
  </code></pre>
  
  <p>If I click on <code>textarea id='aa1'</code>, nothing happens.</p>
  
  <p>If I click on <code>textarea id='aa2'</code> or <code>textarea id='aa3'</code>, 'good' is printed but nothing happens to <code>field.value</code>.</p>
  
  <p>If I click on <code>textarea id='aa4'</code>, 'good' is printed and <code>field.value</code> is ''.</p>
  
  <p>I want a string like <code>AAA\r\nBBBB\\CCC</code> to work like <code>textarea id='aa4</code>.</p>
  
  <p>How can I do that?</p>
  
  <p>I read the post below but it does not seem to help my situation:</p>
  
  <p><a href="https://stackoverflow.com/questions/13843474/javascript-parameter">Javascript parameter</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8793923/javascript-line-break-textarea">Javascript Line Break Textarea</a></p>
  
  <hr>
  
  <p><strong>update:</strong></p>
  
  <p>I've replaced <code>$str1</code> with <code>json_encode($str1)</code>, So <code>alert();</code> works fine now. (Thanks to Jordan Running.)</p>
  
  <p>But the <code>field.value</code> part still does not work.</p>
  
  <p>code refactoring is too hard in my situation... Is there any way to handle the <code>field.value</code> problem without code refactoring?</p>
  
  <p>If quotes corrupt my HTML, I can putting <code>$str1</code> in <code>htmlspecialchars()</code> and displaying it in <code>&lt;textarea&gt;</code>.</p>