Search code examples
jqueryjquery-selectorshtml-entitiesampersand

How to use ampersand (&) symbol inside jQuery selectors


I have a <select> menu with a bunch of <option> tags, all populated from a database table.

I need to use a jquery selector such as $('option[value=d&c]') in order to find an option such as this one:

<option value="d&c">d&amp;c</option>

Note that the value attribute does not have an encoded ampersand (&amp;) in it, just a straight ampersand (&) because of how Zend Framework populates it.

Only problem is that jQuery chokes with the following error:

uncaught exception: Syntax error, unrecognized expression: [value=d&c]

It also won't accept $('option[value=d&amp;c]'). It's the ampersand messing it up, in either case. Does anyone know how to get around this limitation?


Solution

  • You missed the quotes around the value

    This works:

    $('option[value="d&c"]')
    

    jsFiddle