jQuery .selector Property

Beginner
6 min read
Updated: Oct 2026
5 examples
Deprecated

What Is .selector?

.selector was a String on a jQuery collection: the CSS selector passed to jQuery() when the set was created. Example: $("div.item").selector → "div.item". It was never reliable after traversal (.find(), .eq(), …). Prefer keeping your own selector variable or the official plugin parameter pattern.

Remember
// jQuery 1.x / 2.x only
$("div.item").selector   // "div.item"

// After traversal — do not trust it to re-query the same set
$("div.item").eq(0).selector   // still "div.item", but length is 1

// jQuery 3.x
$("div.item").selector   // undefined (removed)

// Modern plugin pattern (official)
$.fn.foo = function (selector, options) { /* … */ };
$("div.bar").foo("div.bar", { dog: "bark" });
NeedUse
Legacy: original query stringcollection.selector (1.x/2.x)
Modern: pass selector to a plugin.foo(selector, options)
Count matches.length
Version string.jquery
Legacy search root.context (also removed)

How to Use .selector (Legacy)

Only for understanding older code or Migrate warnings — not for new features.

  1. Build a set with a selector string: $("div.item").
  2. In jQuery 1.x/2.x, read $set.selector.
  3. Do not re-query with that string after .eq(), .filter(), or .add().
  4. In plugins, accept selector as a method argument instead.
  5. In jQuery 3.x, expect undefined — keep your own variable.
PieceRequired?Role
Selector string in $()Yes (to populate)What .selector stored
jQuery 1.3–2.xYes (to read it)Property exists only there
Explicit plugin argYes (modern)Official replacement
Traversal chains—Make .selector unreliable

Pseudocode

Pseudocode
set = jQuery("div.item")
// legacy: set.selector == "div.item"

set2 = set.eq(0)
// length is 1, but selector may still be "div.item"
// re-running that string would match ALL items — stale!

// modern plugin:
plugin(collection, selector, options)
    // use the passed selector — do not read collection.selector

Cheat Sheet

Keep these facts handy while reading legacy collections.

TopicDetail
TypeProperty → String
AddedjQuery 1.3
DeprecatedjQuery 1.7
RemovedjQuery 3.0
MeaningSelector passed to jQuery() at creation
ReplacementPass selector as a plugin parameter

Live Preview

Toggle between jQuery 1.12 (property exists) and the modern pattern note. Source: jQuery API.

Legacy

Uses jQuery 1.12.4 so .selector still exists. In 3.x it is undefined.

Log
Click “Read .selector”.

jQuery Examples

Examples 1–4 use jQuery 1.12.4. Example 5 uses jQuery 3.7.1 (modern pattern).

Example 1 — Read the Original String

Inspect .selector after a simple CSS selection (jQuery 1.12).

jQuery
var $items = jQuery("div.item");
$("#out").text(
  "length: " + $items.length + "\n" +
  "selector: " + $items.selector
);
Try It Yourself

How It Works

  1. Select. Pass a CSS string to jQuery().
  2. Store. Legacy jQuery keeps that string on the set.
  3. Read. .selector echoes what you typed.
  4. Limit. Only useful before further traversal.

Example 2 — Scoped jQuery(sel, context)

Scoped search still stores the selector string you passed.

jQuery
var sidebar = document.getElementById("sidebar");
var $tags = jQuery(".tag", sidebar);
$("#out").text(
  "count: " + $tags.length + "\n" +
  "selector: " + $tags.selector + "\n" +
  "context id: " + $tags.context.id
);
Try It Yourself

How It Works

  1. Scope. Second argument limits the search root.
  2. Selector. Still ".tag" — the string you passed.
  3. Context. Legacy .context holds the root node.
  4. Today. Keep your own sidebar variable in jQuery 3.

Example 3 — Stale After .eq()

Narrowing the set does not give a selector that rebuilds only that subset.

jQuery
var $items = jQuery("div.item");
var $first = $items.eq(0);
$("#out").text(
  "After $(\"div.item\"): length " + $items.length +
    ", selector " + $items.selector + "\n" +
  "After .eq(0): length " + $first.length +
    ", selector " + $first.selector + "\n" +
  "Re-running that selector would match ALL items"
);
Try It Yourself

How It Works

  1. Full set. Two matches; selector is accurate.
  2. Narrow. .eq(0) keeps one element.
  3. Stale. Selector string still looks like the full query.
  4. Why removed. Official docs: never a reliable rebuild key.

Example 4 — Empty When Wrapping a Node

No selector string was passed — often an empty .selector.

jQuery
var el = document.getElementById("box");
var $fromSel = jQuery("#box");
var $fromNode = jQuery(el);
$("#out").text(
  "From $(\"#box\"): \"" + $fromSel.selector + "\"\n" +
  "From jQuery(el): \"" + $fromNode.selector + "\""
);
Try It Yourself

How It Works

  1. String path. Selector argument is stored.
  2. Node path. Only a DOM reference was given.
  3. Empty. Nothing meaningful to put in .selector.
  4. Lesson. Property only mirrors a string you supplied.

Example 5 — Official Plugin Pattern (jQuery 3)

Pass the selector explicitly — do not read .selector.

jQuery
jQuery.fn.highlightBar = function (selector, options) {
  var color = (options && options.color) || "#fef08a";
  return this.filter(selector).css("background-color", color);
};

jQuery("div.bar").highlightBar("div.bar", { color: "#bbf7d0" });
$("#out").text(
  "Plugin got selector: div.bar\n" +
  ".selector in 3.x: " + (jQuery("div.bar").selector === undefined
    ? "undefined (removed)" : jQuery("div.bar").selector)
);
Try It Yourself

How It Works

  1. API. Plugin requires selector as argument 1.
  2. Call site. Repeat "div.bar" explicitly.
  3. Filter. Use the passed string — not collection metadata.
  4. 3.x. .selector is gone; this pattern still works.

Edge Cases & Pitfalls

Watch for these when reading legacy selector metadata.

3.0

Gone in jQuery 3

.selector is undefined. Do not build new features on it.

stale

Unreliable after traversal

.eq(), .filter(), .add(), and friends change the set without a trustworthy rebuild string.

empty

DOM / HTML wrappers

$(element) or $("<div>") often yields an empty selector string.

plugin

Ask for the string

Official fix: $.fn.foo = function(selector, options) { … } and repeat the selector at the call site.

Browser Support

.selector existed on collections in jQuery 1.3–2.x. It was deprecated in 1.7 and removed in jQuery 3.0. Not available in modern jQuery releases.

✓ Deprecated · Removed 3.0

.selector

Historical collection metadata only. Use jQuery 1.12.x / 2.2.x for legacy demos. New code should pass selector strings explicitly.

N/A Modern 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
.selector Removed

Bottom line: Do not use in new projects. Migrate plugins to an explicit selector parameter. Keep studying it to understand older codebases and Migrate warnings.

Performance Notes

ApproachCostBest for
Read .selector (1.x/2.x)FreeLegacy debugging only
Re-query with stale stringWrong resultsAvoid
Explicit plugin selector argFreeModern plugins
Own variable for the queryFreeWidgets and modules

The cost is correctness, not CPU. Storing your own selector string (or accepting it as a parameter) is clearer and works in jQuery 3.

Key Takeaways

  • Role: original selector string at collection creation.
  • Unreliable: traversal can make it a bad rebuild key.
  • Removed: deprecated 1.7, gone in jQuery 3.0.
  • Replace: pass the selector as a plugin argument.

One line: .selector remembered creation-time query text — do not use it in jQuery 3; pass selectors explicitly.

Frequently Asked Questions

.selector is a String on jQuery collections (1.3–2.x) that stored the CSS selector passed to jQuery() when the set was first created — for example $("div.item").selector returns "div.item".
No. Official docs warn it was never reliable after traversal. Methods like .find(), .eq(), .filter(), and .add() change the set without guaranteeing a selector that would rebuild the same set.
No. It was deprecated in jQuery 1.7 and removed in jQuery 3.0 along with .context. Pass the selector string explicitly into plugins instead.
Often an empty string. When you pass a DOM node, HTML string, or callback — not a selector — jQuery had nothing to store.
.selector held the query string ("#menu li"). .context held the DOM root that limited the search. Both were creation-time metadata removed in jQuery 3.0.
Require the selector as a parameter: $.fn.foo = function(selector, options) { ... }; then call $("div.bar").foo("div.bar", { dog: "bark" }); — repeat the selector explicitly.

Next: .jquery

Read the version string that still ships with every collection.

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

10 people found this page helpful