title: Uncaught SyntaxError: Unexpected identifier - JS Error, when php put's a single qutoe or apostrophe in function parameter
body: <p>Alright I am using AJAX to generate a list of potential customer matches from a WebService.  Everything is working except for this part in my pHp ...</p>
  
  <pre><code>$html = "&lt;h3&gt; Search Results: ";
  
          if(!is_array($searchResults))
          {
              $html .= $searchResults . "&lt;/h3&gt;";
          }
          else{
              $html .= "&lt;/h3&gt;&lt;ul&gt;";
              foreach($searchResults as $result)
              {
                  $html .= '&lt;li&gt; &lt;a href="#" onclick="selectCustomer('. "'" . htmlentities((string)$result-&gt;entityId, ENT_QUOTES) . "'" . ')"&gt;' . $result-&gt;entityId . "&lt;/a&gt;&lt;/li&gt;";
              }
              $html .= "&lt;/ul&gt;";
          }
  
          echo $html;
  </code></pre>
  
  <p>Here is my select JavaScript selectCustomer function:</p>
  
  <pre><code>    &lt;script&gt;
  function selectCustomer(selectedCustomer){
              $.ajax({
                  type: 'GET',
                  url: "&lt;?php echo JURI::base() . "index.php?option=com_sample&amp;view=place_order&amp;task=getCustomer&amp;customerName=";?&gt;" + encodeURIComponent(selectedCustomer),
                  beforeSend:function(){
                      $('#CustomerInfo').html('&lt;div class="loading"&gt;&lt;img src="&lt;?php echo JURI::base() . "components/com_joomsuite/assets/images/ajax-loader.gif"?&gt;" alt="Loading..." /&gt;&lt;/div&gt;');
                  },
                  success:function(data){
                      $('#CustomerInfo').html(data);
                      $('#customerEmail').val($('#customerEmailAJAX').html());
                      $('#customerName').val($('#customerNameAJAX').html());
                  },
                  error:function(){
                      $('#ajax-panel').html('&lt;p class="error"&gt;&lt;strong&gt;Oops!&lt;/strong&gt; Try that again in a few moments.&lt;/p&gt;');
                  }
              });
          }
  &lt;/script&gt;
  </code></pre>
  
  <p>You may have noticed a little pHp in my JS using Joomla :P... this function is used successfully in 2 other situations.  It only fails when it is called from the onclick that gets displayed via AJAX. </p>
  
  <p>Chrome keeps throwing the "Uncaught SyntaxError: Unexpected identifier" but no line #.  I'm assuming that it has something to do with the onclick call. </p>
  
  <p>I went into chrome's inspect element and found this:</p>
  
  <pre><code>&lt;a href="#" onclick="selectCustomer('13064 Moto's Burger Palace')"&gt;13064 Moto's Burger Palace&lt;/a&gt;
  </code></pre>
  
  <p>So here is the apparent problem right?  Its the same old "You put your apostrophe by my single quote" argument from when you were a kid.  But shouldn't the htmlentities() function be preventing this... well no because the apostrophe gets rendered by chrome (the browser).  </p>
  
  <p>So my question is (finally) what can I change in my php function that will allow the onclick to run my selectCustomer function and correctly pass a string that may or may not contain an apostrophe...</p>
  
  <p>I would prefer to change this line (but am willing to accept other possibilities):</p>
  
  <pre><code>$html .= '&lt;li&gt; &lt;a href="#" onclick="selectCustomer('. "'" . htmlentities((string)$result-&gt;entityId, ENT_QUOTES) . "'" . ')"&gt;' . $result-&gt;entityId . "&lt;/a&gt;&lt;/li&gt;";
  </code></pre>