jQuery .context

Beginner
5 min read
Updated: Nov 2026
3 examples
Properties

Definition

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

Syntax

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

jQuery
$("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
CallWhat it does
SelectorWhat to match
Second arg (context)Search root; defaults to document
.contextStored root on the collection (removed 3.0)
Return typeNative DOM Element or Document

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

Parameters

ParameterDescription
contextUsed in Second arg (context). Search root; defaults to document

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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

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

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