Skip to content

Commit 786fac3

Browse files
schlimmchenKrinkle
andauthored
FAQ: Escape forwards slashes and point to jQuery.escapeSelector
Closes #765. Co-authored-by: Timo Tijhof <krinkle@fastmail.com>
1 parent 252f982 commit 786fac3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

page/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"title": "How do I select an element by an ID that has characters used in CSS notation?"
33
}</script>
44

5-
Because jQuery uses CSS syntax for selecting elements, some characters are interpreted as CSS notation. In order to tell jQuery to treat these characters literally rather than as CSS notation, they must be escaped by placing two backslashes in front of them.
6-
7-
See the [Selector documentation](http://api.jquery.com/category/selectors/) for further details.
5+
Because jQuery uses CSS syntax for selecting elements, some characters are interpreted as CSS notation. In order to tell jQuery to treat these characters literally rather than as CSS notation, they must be escaped by placing two backslashes in front of them. See the [Selector documentation](http://api.jquery.com/category/selectors/) for further details.
86

97
```
108
// Does not work:
@@ -20,18 +18,20 @@ $( "#some.id" )
2018
$( "#some\\.id" )
2119
```
2220

23-
The following function takes care of escaping these characters and places a "#" at the beginning of the ID string:
21+
On jQuery version 3.0 or later, the function [jQuery.escapeSelector()](https://api.jquery.com/jQuery.escapeSelector/) should be used to escape such selectors.
22+
23+
Otherwise, the following function takes care of escaping these characters and places a "#" at the beginning of the ID string for convenience:
2424

2525
```
26-
function jq( myid ) {
26+
function escapeSelector( myid ) {
2727
28-
return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );
28+
return "#" + myid.replace( /(\/|:|\.|\[|\]|,|=|@)/g, "\\$1" );
2929
3030
}
3131
```
3232

3333
The function can be used like so:
3434

3535
```
36-
$( jq( "some.id" ) )
36+
$( escapeSelector( "some.id" ) )
3737
```

0 commit comments

Comments
 (0)