jQuery .context

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

What Is .context?

The DOM node originally passed to jQuery() as the search root. If none was passed, it is usually the document. Added in jQuery 1.3. It is a property — not a method — so write $("li").context, not $("li").context().

Remember
$("ul").context              // usually document
$("ul", document.body).context // BODY element

// Second arg still scopes the search in jQuery 3.x
// Reading .context back only works on 1.x / 2.x
NeedUse
Scope a search$(sel, container)
Read root (legacy)collection.context (1.x / 2.x)
Modern delegation.on(ev, sel, fn)
Legacy consumer.live() (removed)
Related removed API.selector
Match count.length

How to Use .context

Pass an optional root into jQuery(), then (on 1.x / 2.x) read it back from the collection.

  1. Optionally pass a DOM node as the second argument: $(sel, root).
  2. jQuery searches only inside that root (or the document if omitted).
  3. On jQuery 1.x / 2.x, read .context — a native Element or Document.
  4. In jQuery 3.x, keep the root variable yourself — do not read .context.
  5. For events, bind with .on() on a stable parent instead of removed .live().
PieceRequired?Role
SelectorYesWhat to match
Second arg (context)OptionalSearch root; defaults to document
.contextLegacyStored root on the collection (removed 3.0)
Return type—Native DOM Element or Document

Pseudocode

Pseudocode
root = optional DOM node (or document)
set = jQuery(selector, root)
  → search only inside root
  → (jQuery 1.x / 2.x) set.context = root

// Modern (jQuery 3.x):
root = document.getElementById("panel")
set = $(selector, root)   // scoping still works
keep root in your variable  // do not read .context

// Events:
$(root).on("click", ".child", handler)

Cheat Sheet

Keep these facts handy when reading legacy collections.

TopicDetail
TypeDOM Element or Document (not a jQuery object)
SincejQuery 1.3
DeprecatedjQuery 1.10 / 2.0
RemovedjQuery 3.0
Defaultdocument when no second arg
Access$(...).context (1.x / 2.x only)
vs 2nd argYou pass it in; .context stored it
Official docsapi.jquery.com/context/

Live Preview

Pick a search root and see what .context returns — same idea as the official jQuery API demo (runs on jQuery 1.12.4).

Demo of .context on jQuery 1.12.4 — removed in jQuery 3.0.

Result
Click “Read .context” to start.

jQuery Examples

Short, copy-ready snippets. Examples 1–4 need jQuery 1.12.4 for .context. Example 5 uses modern jQuery 3.x with .on().

Example 1 — Official: Default vs Body Context

Append lines showing .context as a string and as nodeName.

jQuery
$("ul")
  .append("<li>context: " + $("ul").context + "</li>")
  .append("<li>nodeName: " + $("ul", document.body).context.nodeName + "</li>");
Try It Yourself

How It Works

  1. Default. $("ul") has no second arg → .context is the document.
  2. Stringify. Coercing the document to a string shows [object HTMLDocument].
  3. Body root. Pass document.body → .context is the body element.
  4. nodeName. .context.nodeName is "BODY" (uppercase in HTML).

Example 2 — Scoped Search Inside a Container

Pass a DOM element as the second argument — .context equals that element.

jQuery
var panel = document.getElementById("panel-a");
var $tags = $(".tag", panel);

$("#out").text(
  "count=" + $tags.length +
  ", id=" + $tags.context.id +
  ", same=" + ($tags.context === panel)
);
Try It Yourself

How It Works

  1. Root. Grab the container DOM node once and keep it in panel.
  2. Scope. $(".tag", panel) matches only tags inside that panel.
  3. Mirror. On 1.x / 2.x, .context is the same node as panel.
  4. Today. In jQuery 3.x, keep using the second arg — just rely on panel, not .context.

Example 3 — Same Selector, Different Context

Identical selectors can return different counts and different .context values.

jQuery
var main = document.getElementById("main");
var $all = $("button");
var $inMain = $("button", main);

$("#out").text(
  "all=" + $all.length + " (" + $all.context.nodeName + ")\\n" +
  "scoped=" + $inMain.length + " (" + $inMain.context.id + ")"
);
Try It Yourself

How It Works

  1. Global. $("button") searches the whole document → two matches.
  2. Scoped. $("button", main) searches only inside #main → one match.
  3. Roots differ. Global .context is the document; scoped .context is #main.
  4. Lesson. Scoping prevents accidental matches elsewhere on the page.

Example 4 — .context After .add()

Merged elements keep the original set’s context — not each member’s root.

jQuery
var zoneA = document.getElementById("zone-a");
var zoneB = document.getElementById("zone-b");
var $fromA = $(".item", zoneA);
var $merged = $fromA.add($(".item", zoneB));

$("#out").text(
  "fromA=" + $fromA.context.id +
  ", merged=" + $merged.context.id +
  ", length=" + $merged.length
);
Try It Yourself

How It Works

  1. First set. Select items inside #zone-a → .context is that zone.
  2. Merge. .add() brings in items from #zone-b.
  3. Same root. Merged collection still reports the original .context (zone-a).
  4. Caveat. Do not assume .context describes every element after merges.

Example 5 — Modern Delegation Without .context

Replace removed .live() with .on() on a stable parent — works in jQuery 3.x.

jQuery
$("#feed").on("click", ".item", function () {
  $("#status").text("Selected: " + $(this).text());
});

$("<div>", { "class": "item", text: "Post 3" })
  .appendTo("#feed");
Try It Yourself

How It Works

  1. Root. Bind on #feed — that parent is your context.
  2. Filter. The ".item" selector matches clicked descendants.
  3. Dynamic. Items added later still work because events bubble to #feed.
  4. No metadata. You never need .context or removed .live().

Edge Cases & Pitfalls

Watch for these when reading or migrating .context.

removed

Undefined in jQuery 3.x

.context does not exist on modern collections. Keep your own container variable.

merge

.add() does not update context

The property still reflects the original set’s root after merges.

type

Native DOM node

Use .nodeName / === — not jQuery methods on .context.

events

Not the same as this

Inside a handler, this is the event target element — not the collection’s search root.

Browser Support

.context existed on jQuery 1.3–2.x collections. It was removed in jQuery 3.0. The scoped constructor form jQuery(selector, context) remains supported.

✓ Removed · jQuery 3.0

.context

Available on jQuery 1.12.x and 2.2.x only. jQuery 3.x has no .context — use an explicit element reference and .on() delegation.

Legacy jQuery 1.x / 2.x
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
.context Removed 3.0

Bottom line: Scoped selection with the second jQuery() argument is still valid. Reading .context back from the collection is what was removed — keep the container variable yourself.

Performance Notes

ApproachCostBest for
Read .contextFreeLegacy debugging (1.x / 2.x)
$(sel, root)Cheaper searchWidget-scoped selectors
Global $(sel)Can be costlyAvoid on large documents when a root exists

The property itself was cheap metadata. Real savings come from scoping selectors with a second argument so jQuery searches a smaller subtree.

Key Takeaways

  • Role: DOM node — the search root for that collection.
  • Default: usually document when no second arg.
  • Status: removed in jQuery 3.0 — keep your own root variable.
  • Modern: scope with $(sel, root); delegate with .on().

One line: .context stored the search root — today you keep that root yourself and still pass it as the second argument.

Frequently Asked Questions

.context is a read-only property on a jQuery collection (1.3–2.x). It holds the DOM node that was passed as the second argument to jQuery() when the set was created. If none was supplied, it is usually the document.
After selecting elements, access collection.context — it is a native DOM Element or Document, not a jQuery object. Example: var root = $(".item", container).context; console.log(root.id).
No. .context was deprecated in jQuery 1.10 / 2.0 and removed in jQuery 3.0. Use jQuery 1.12.x or 2.2.x for legacy demos, or keep an explicit element reference instead of reading .context.
You pass the search root into jQuery(selector, context) to limit the search. On jQuery 1.x/2.x, that node was also stored on the collection as .context. The second argument still works in jQuery 3.x — only reading .context back was removed.
Usually not. .context reflects the original creation context of that set. Elements merged later with .add() may come from a different root; the property does not track each member individually.
Keep a variable pointing to the container you care about, pass it to jQuery() as the second argument when needed, and bind delegated events with .on(events, selector, handler) on a stable parent.

Next: jQuery .selector Property

Learn the original selector string metadata and why it was removed in 3.0.

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