Ticket #8253 (closed bug: wontfix)
Selectors documentation says names must be double backslash-encoded
| Reported by: | Chealer | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | selector | Version: | 1.5 |
| Keywords: | Cc: | kswedberg | |
| Blocking: | Blocked by: |
Description
http://api.jquery.com/category/selectors/ says:
If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;?@[\]`{|}~ ) as a literal part of a name, you must escape the character with two backslashes:
. For example, if you have an an element with id="foo.bar", you can use the selector $("#foo
.bar").
Of course, the selector needs a single backslash for escaping. The paragraph is confused because in JavaScript, backslashes also escape string literals, and selectors are usually built from JavaScript string literals. To use the same example, if you have an element with id="foo.bar", you should use the selector "#foo\.bar", but that selector is written "#foo
.bar" in JavaScript.
The nuance can be tricky, and PHP folks, who have a similar situation with regular expressions, put an explaining note, as can be seen on http://ca.php.net/manual/en/regexp.reference.escape.php It's quite important to have that note; a simple fix without a similar note would probably be as confusing as the current paragraph is incorrect.
You'll also note that the text contains a double "an" ("an an element").
Change History
comment:1 Changed 2 years ago by jitter
- Cc kswedberg added
- Priority changed from undecided to low
- Status changed from new to open
- Component changed from unfiled to selector
comment:2 Changed 2 years ago by addyosmani
- Status changed from open to closed
- Resolution set to wontfix
After careful consideration, in my opinion adding this note would increase confusion, particularly amongst those who aren't as familiar with the nuances of JavaScript string literals (on one hand, we're already trying to get across the idea that two backslashes are needed for meta-characters, do we really need to add another thing for beginners to understand which they may not use in the same note?).
I did write out a version with Chealer's changes in there, but again..I can't see this adding as much value as it would confusion (with beginners in mind).
The 'an an' change has however been made. If kswedberg has a different opinion on this and feels that we should reopen and update to include a note, I'll totally respect that.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

I don't know, I could imagine adding this note to cause more confusion but it's surely worth a consideration. Bringing in our documentation folks on this one.