jQuery .selector Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Properties

Definition

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

Syntax

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

jQuery
// 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" });
CallWhat it does
Selector string in $()What .selector stored
jQuery 1.3–2.xProperty exists only there
Explicit plugin argOfficial replacement
Traversal chainsMake .selector unreliable

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

Parameters

ParameterDescription
Step 1Build a set with a selector string: $("div.item").
Step 2In jQuery 1.x/2.x, read $set.selector.
Step 3Do not re-query with that string after .eq(), .filter(), or .add().

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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 Property — Version String

Read the version string that still ships with every collection.

.jquery Property — Version String 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.

10 people found this page helpful