Bug Tracker


Ticket #7119 (closed bug: worksforme)

Opened 4 years ago

Last modified 4 years ago

Escaping of apostroph(') in selector doesn't work properly

Reported by: pxx Owned by:
Priority: low Milestone: 1.4.3
Component: selector Version: 1.4.2
Keywords: escaping, apostroph, context Cc:
Blocking: Blocked by:


According to documentation, all special symbols in selector should be escaped with 2 backslashes:

See the following example: <select><option value="apo'stroph">apo'stroph</option></select>

Query $("option[value=apo
'stroph]") works fine and returns expected option But when I need to define context in query like $("option[value=apo
'stroph]", 'select') or $('select').find("option[value=apo
'stroph]") it crashes with following exception: Syntax error, unrecognized expression: value=apo'stroph]

Change History

comment:1 Changed 4 years ago by addyosmani

  • Status changed from new to closed
  • Resolution set to worksforme

In order to make the given selector work, you need to double-escape the value of the option you're trying to select (in this case a little differently to what you originally did).

Whilst you are doing this inside of [value=X], in order for this to correctly work without breaking you need to be double-escaping your selector in a way that maintains clean-syntax as follows:

var val = "apo
'stroph"; var test = $('option[value="'+val+'"]'); console.log(test.length); works console.log(test.val()); works

A live test case of this working can be found here:  http://jsfiddle.net/VYtv4/4/

comment:2 Changed 4 years ago by pxx

I am sorry, I didn't know about jsFiddle and issue parser had cut out slashes. Here is my live test case:  http://jsfiddle.net/VYtv4/5/ now, just FYI, because it works in newest jQuery version (1.4.3)

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.


Add a comment

Modify Ticket

as closed

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.