Error: Syntax error, unrecognized expression: #sub_account1 option[value=HD&NO]
<p>I have a PHP script that queries a DB and returns json_encoded data to a javascript/jquery file that processes the returned data into dynamic options in a drop-down select box. </p>

<p>This has been working fine.  A recently added ampersand, '&amp;', into a field in our database: ereqs.sub_account_code -> 'HD&amp;NO' is causing a syntax error viewed via FireBug when loading the page. When this value is returned by the query below, a jquery/js error interrupts the response and no values get added to the options in the select box. </p>

<blockquote>
  <p>Error: Syntax error, unrecognized expression: #sub_account1 option[value=HD&amp;NO]</p>
</blockquote>

<p>javascript file: (json call)</p>

<pre><code>function get_sub_account_codes(instance, code){
  // json call to get sub-account-codes.
  $.getJSON("get-sub-account-codes.html",{code:code}, function(data){
     // empty html drop-down of options that may be showing
     $('#sub_account' + instance).html(''); // .empty()
     $('#sub_account' + instance).val('');
     // build the drop-down options
     $.each(data, function(key, value){
        $("#sub_account" + instance).append("&lt;option value=\""+value.SUB_ACCOUNT_CODE+"\"&gt;"+value.SUB_ACCOUNT_CODE+"-"+value.SUB_ACCOUNT_NAME+"&lt;/option&gt;");         
     });
  });
}
</code></pre>

<p>get-sub-account-codes.html  PHP code to get data.  Called from js file</p>

<pre><code>$data = array();
$dbh = SHA_DB::getConnection('webadmin');
$sql = "select distinct sub_account_code, sub_account_name
      from ereqs.kuali_account_codes 
      where account_code = upper(?) and
            sub_account_code is not null
      order by sub_account_code";
$results = $dbh-&gt;queryChecked($sql, array($code));
while($d = $results-&gt;fetchRow(DB_FETCHMODE_ASSOC))
{
    $data[] = $d;
}
# return json encoded string.
print json_encode($data);
</code></pre>

<p>Removing the ampersand '&amp;' is not a viable option.</p>

<p>Is there a way to check for the &amp; in the PHP file and escape it somehow before it is sent back in the <code>json_encode($data)</code>?  Or should this be checked for and changed in the javascript code?</p>

<p>Thanks for any tips.</p>