• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Handling apostrophe in a Attribute Equals Selector

 
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am facing trouble in handling a situation where value of attribute equals selector may contains an apostrophe. Below is a detailed explanation of my problem :

$('option[value='+l_val[l_i].split("#")[0]+']',$('#fldAllQuestions')).remove();

Here l_val[l_i].split("#")[0] may contain a string like "How many Brother's do you have?". There is a listbox containing question list. Above code removes questions from that list. If the Question to be removed has no apostrophe then it works fine. However with an apostrophe it give error,
"Error: Syntax error, unrecognized expression: option[value=How many Brother's do you have?]"

I am unable to escape the apostrophe.

Please suggest a fix for same.

Thanks
Suvo
 
author
Posts: 297
5
Android Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really clear here - why are you unable to escape the apostrophe? Or are you asking how to escape the apostrophe? (BTW, with a backslash: '\'')

Also note: in the sentence in your example code, there shouldn't be an apostrophe. It should be a plural "Brothers", it is not the possessive "Your Brother's option".

OK, I've tried your example now, the fundamental problem is that this is not a valid CSS selector:

It produces an error like this in the browser console:

However, this is a valid selector:

The slightly counter-intuitive thing when converting this to a JavaScript statement is that what quotes you have to escape depends on what quotes you're using to wrap the whole string. Because your string is wrapped in double quotes, it's those that need to be escaped:

Note that I changed the sentence to one in which the apostrophe is grammatically appropriate.
reply
    Bookmark Topic Watch Topic
  • New Topic