jQuery.escapeSelector(selector) (alias $.escapeSelector) escapes characters that have special meaning in CSS so you can use them as literal class or ID text (since jQuery 3.0).
Remember
$.escapeSelector("#target") // "\#target"
"." + $.escapeSelector(".box") // ".\.box"
// Still prepend . or # yourself — escape only the name.
Escape the identifier, then build the full selector string.
Start with a class or ID string that may contain CSS punctuation.
Call $.escapeSelector(name) to escape special characters.
Prepend "." or "#" to form a complete selector.
Pass that string to $(), .find(), or another selector API.
Piece
Required?
Role
selector
Yes
String to escape (identifier fragment)
Return value
—
Escaped string safe inside a CSS selector
Pseudocode
Pseudocode
function escapeSelector(text):
for each character in text:
if character is special in CSS:
prefix it with a backslash
return escapedText
selector = "." + escapeSelector(className)
elements = $(selector)
Reference
Cheat Sheet
Keep these facts handy when building dynamic selectors.
Variable name.btn:primary contains :, which is special in CSS.
Escape.$.escapeSelector(name) makes the colon literal.
Select. Prepend "." and query with $().
Habit. Escape any untrusted or generated class/ID before concatenating.
Edge Cases & Pitfalls
Watch these before splicing names into selectors.
prefix
Still add . or # yourself
escapeSelector escapes the name only. Forgetting the prefix selects nothing useful.
version
Needs jQuery 3.0+
Older jQuery builds do not include this helper. Upgrade or use a polyfill / CSS.escape().
plain
Safe names still work
Escaping "card" is harmless — you get the same string back when nothing special is present.
input
Never concat raw user input
Always escape class/ID fragments from forms or APIs before building $("..." + value).
Compatibility
Browser Support
jQuery.escapeSelector() has shipped since jQuery 3.0. It works in all modern browsers with jQuery 3+ loaded and acts as a reliable shim for CSS.escape().
✓ jQuery 3.0+
jQuery.escapeSelector()
Supported everywhere jQuery 3+ runs in the browser. Use it when class or ID names include CSS punctuation.
100%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
$.escapeSelector()Excellent
Bottom line: Call $.escapeSelector(name), then prepend . or #. Prefer this helper over hand-rolled backslashes inside jQuery apps.
Analysis
Performance Notes
Approach
Cost
Best for
$.escapeSelector(str)
O(n)
Dynamic class / ID strings
CSS.escape(str)
O(n)
Native code outside jQuery
Manual replace
O(n)
Avoid — easy to miss characters
Escaping is cheap compared to DOM queries. Escape once when you build the selector; reuse the resulting string if you query repeatedly.
Remember
Key Takeaways
Role: make CSS-special characters literal inside selectors.
Build:"." + $.escapeSelector(name) or "#" + ….
Since: jQuery 3.0 — static utility, returns a string.
Native: similar idea to CSS.escape() outside jQuery.
One line: escape the name with $.escapeSelector(), then prepend . or # before you query.
Frequently Asked Questions
jQuery.escapeSelector(selector) returns a string where CSS-special characters are escaped so they act as literal text inside a selector. Use it on the identifier part of a class or ID — then still prepend . or # yourself.
It was added in jQuery 3.0 as a static utility: $.escapeSelector(str). It is not deprecated and remains the idiomatic way to build safe dynamic selectors in jQuery apps.
Both escape identifier characters for CSS. CSS.escape() is a native browser API. $.escapeSelector() is a jQuery shim so the same escape works in all browsers jQuery supports. Prefer $.escapeSelector() inside jQuery code.
Yes. escapeSelector only escapes the name. Build class selectors with "." + $.escapeSelector(name) and ID selectors with "#" + $.escapeSelector(id).
No. Call jQuery.escapeSelector(str) or $.escapeSelector(str). It returns a string — it is not chained on a jQuery collection.
Whenever a class or ID may contain #, ., :, [, ], spaces, or other CSS punctuation — CMS names, user-generated tokens, or literal names like .box and #target.