jQuery.escapeSelector() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.escapeSelector("#target")  // "\#target"
"." + $.escapeSelector(".box")  // ".\.box"

// Still prepend . or # yourself — escape only the name.
CallWhat it does
selectorString to escape (identifier fragment)
Return valueEscaped string safe inside a CSS selector

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with a class or ID string that may contain CSS punctuation.
Step 2Call $.escapeSelector(name) to escape special characters.
Step 3Prepend "." or "#" to form a complete selector.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: unique() Method

Learn the shared empty function placeholder for optional callbacks.

unique() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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