jQuery.escapeSelector() Method

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

What Is jQuery.escapeSelector()?

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.
NeedUse
Safe class fragment"." + $.escapeSelector(name)
Safe ID fragment"#" + $.escapeSelector(id)
Search descendants$("div").find(sel)

How to Use jQuery.escapeSelector()

Escape the identifier, then build the full selector string.

  1. Start with a class or ID string that may contain CSS punctuation.
  2. Call $.escapeSelector(name) to escape special characters.
  3. Prepend "." or "#" to form a complete selector.
  4. Pass that string to $(), .find(), or another selector API.
PieceRequired?Role
selectorYesString 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)

Cheat Sheet

Keep these facts handy when building dynamic selectors.

TopicDetail
SyntaxjQuery.escapeSelector(selector)
SincejQuery 3.0
ReturnsEscaped selector string
KindStatic utility (not chainable)
Native cousinCSS.escape()
Typical useLiteral ., #, : in names

Live Preview

Pick a sample name and see the escaped string plus a ready-to-use selector.

Browser demo of $.escapeSelector() — same idea as the examples below.

Result
input → #target
escaped → \#target
ID selector → #\#target

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Escape an ID Containing #

Official style: escape a string that starts with a hash.

jQuery
console.log($.escapeSelector("#target"));
// "\#target"
Try It Yourself

How It Works

  1. Input. Pass "#target" — the # normally starts an ID selector.
  2. Escape. jQuery prefixes special characters with a backslash.
  3. Result. You get a string safe to splice into a larger selector.
  4. Next step. Usually build "#" + $.escapeSelector(id) for a full ID query.

Example 2 — Find Elements With Class .box

Official pattern: escape a class name that includes a leading dot.

jQuery
// HTML: <div><span class=".box">ok</span></div>
var $hit = $("div").find("." + $.escapeSelector(".box"));
console.log($hit.text());  // "ok"
Try It Yourself

How It Works

  1. Literal class. The element’s class attribute is .box, not box.
  2. Escape name. $.escapeSelector(".box") turns the dot into literal text.
  3. Build selector. Prepend "." so Sizzle looks for that class name.
  4. Find. .find() searches only inside the div.

Example 3 — Select an Element Whose ID Is #target

Combine # with an escaped ID fragment.

jQuery
// HTML: <p id="#target">found</p>
var sel = "#" + $.escapeSelector("#target");
console.log($(sel).text());  // "found"
Try It Yourself

How It Works

  1. Weird ID. The element’s id attribute is literally #target.
  2. Escape first. Escape the ID string, then prepend another #.
  3. Query. $(sel) matches that one element.
  4. Without escape. $("#target") would look for id="target" instead.

Example 4 — Escape a Dynamic Class Name

Safe when names come from variables (colons, dots, and more).

jQuery
// HTML: <span class="btn:primary">go</span>
var name = "btn:primary";
var $el = $("." + $.escapeSelector(name));
console.log($el.text());  // "go"
Try It Yourself

How It Works

  1. Variable name. btn:primary contains :, which is special in CSS.
  2. Escape. $.escapeSelector(name) makes the colon literal.
  3. Select. Prepend "." and query with $().
  4. 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).

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.escapeSelector() Excellent

Bottom line: Call $.escapeSelector(name), then prepend . or #. Prefer this helper over hand-rolled backslashes inside jQuery apps.

Performance Notes

ApproachCostBest for
$.escapeSelector(str)O(n)Dynamic class / ID strings
CSS.escape(str)O(n)Native code outside jQuery
Manual replaceO(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.

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.

Next: noop()

Learn the shared empty function placeholder for optional callbacks.

noop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful