jQuery contains() Method

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

What Is contains()?

jQuery.contains(container, contained) (alias $.contains) returns true when contained is a descendant of container — direct child or nested deeper (since jQuery 1.4).

Remember
$.contains(container, contained)  → true | false

$.contains(document.documentElement, document.body)  // true
$.contains(document.body, document.documentElement)  // false

// Unwrap jQuery collections:
$.contains($("#box")[0], $("#item")[0])
NeedUse
Is node A inside B?$.contains(B, A)
Search by selector$box.find("span")
Filter parents that have a match$els.has("span")

How to Use contains()

Get two element nodes, then ask if the second lives inside the first.

  1. Pick the container element (ancestor candidate).
  2. Pick the contained element (possible descendant).
  3. If you have jQuery objects, unwrap with [0].
  4. Call $.contains(container, contained) and branch on true / false.
PieceRequired?Role
containerYesDOM element that may contain the other
containedYesDOM element that may be a descendant
Return—Boolean

Pseudocode

Pseudocode
function contains(container, contained):
    if contained is not an element node:
        return false
    // walk ancestors of contained (or use native check)
    return contained is inside container (not equal to container)

// Official demo:
$.contains(document.documentElement, document.body)  → true
$.contains(document.body, document.documentElement)  → false

Cheat Sheet

Keep these facts handy while testing DOM ancestry.

TopicDetail
SyntaxjQuery.contains(container, contained)
SincejQuery 1.4
ArgumentsDOM Element nodes only
Self check$.contains(el, el) → false
Text nodesSecond arg text/comment → false
jQuery objectsUnwrap with [0] first
Native cousincontainer.contains(contained) (broader node types)

Live Preview

Pick a container and a target from a small tree, then run $.contains().

Browser demo of $.contains(container, contained) — same idea as the examples below.

Result
$.contains(document.documentElement, document.body)
→ true

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Official documentElement Check

The jQuery API demo in two lines.

jQuery
console.log($.contains(document.documentElement, document.body)); // true
console.log($.contains(document.body, document.documentElement)); // false
Try It Yourself

How It Works

  1. Container first. document.documentElement is the <html> element.
  2. Descendant. document.body sits under <html> → true.
  3. Reversed. Asking if <html> is inside <body> → false.
  4. Order matters. Always pass ancestor, then possible child.

Example 2 — Direct Child and Grandchild

Any depth of nesting still counts as contained.

jQuery
// <div id="box"><div id="item"><span id="label">Hi</span></div></div>
var box = document.getElementById("box");
var item = document.getElementById("item");
var label = document.getElementById("label");

console.log($.contains(box, item));   // true
console.log($.contains(box, label));  // true
Try It Yourself

How It Works

  1. Tree. box → item → label.
  2. Direct child. item is inside box → true.
  3. Deeper. label is also inside box → true.
  4. No distance limit. Depth does not matter — only ancestry.

Example 3 — Siblings and Self Return false

Only true descendants count — not peers or the same node.

jQuery
// <div id="box-a">A</div><div id="box-b">B</div>
var boxA = document.getElementById("box-a");
var boxB = document.getElementById("box-b");

console.log($.contains(boxA, boxB));  // false
console.log($.contains(boxA, boxA));  // false
Try It Yourself

How It Works

  1. Siblings. box-a and box-b share a parent but neither contains the other.
  2. Self. An element is not considered a descendant of itself → false.
  3. Click-outside tip. Test $.contains(menu, event.target) — clicks on the menu root itself are outside this helper’s “descendant” rule; include an extra menu === event.target check when needed.
  4. Boolean only. No exception — unrelated pairs simply return false.

Example 4 — Unwrap jQuery Objects

Always pass raw elements from a collection.

jQuery
// <nav id="menu"><a id="menu-link" href="#">Link</a></nav>
var $menu = $("#menu");
var $link = $("#menu-link");

console.log($.contains($menu[0], $link[0]));  // true
Try It Yourself

How It Works

  1. Select. $("#menu") and $("#menu-link") are jQuery collections.
  2. Unwrap. [0] reads the first matched DOM element.
  3. Call. $.contains($menu[0], $link[0]) gets native elements as required.
  4. Result. The link sits inside the nav → true.

Edge Cases & Pitfalls

Check these before using $.contains() in UI logic.

types

jQuery objects are not valid arguments

Pass $el[0], not $el. The first argument must be a DOM element.

nodes

Text and comment nodes return false

Only element nodes count for contained. Prefer event.target when it is an element, or walk up with .closest().

self

An element does not contain itself

For click-inside checks, use container === target || $.contains(container, target).

native

Native Node.contains() is broader

Modern browsers support element.contains(node) for any node type. Keep $.contains when you already use jQuery and want element-only checks.

Browser Support

jQuery.contains() needs a browser DOM. It has been available since jQuery 1.4+ in all modern browsers — it is not supported in Node.js, Bun, or Deno without a DOM.

✓ jQuery 1.4+ · Browsers only

jQuery.contains()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery loaded. No Node.js support — this helper checks real DOM ancestry.

100% Browser 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
$.contains() Browsers

Bottom line: Use $.contains(container, contained) in the browser with DOM elements. Unwrap jQuery objects with [0] first.

Performance Notes

ApproachCostBest for
$.contains(a, b)O(depth)Known element pairs
$a.find(sel)HigherSearch when you do not have the child node yet
$a.has(sel)HigherFilter a collection by descendant match

Prefer $.contains when both nodes are already known — it is a targeted ancestry check, not a full selector scan.

Key Takeaways

  • Role: $.contains(container, contained) → boolean ancestry.
  • Args: DOM elements only — unwrap with [0].
  • Self: an element does not contain itself.
  • Browser: DOM only — not for Node.js.

One line: ask $.contains(ancestor, descendant) with raw elements — unwrap jQuery, and remember self-checks are false.

Frequently Asked Questions

jQuery.contains(container, contained) returns true when contained is a descendant of container — a direct child or nested deeper. It returns false when the nodes are unrelated, siblings, or when contained is not inside container.
No. Both arguments must be native DOM elements. Unwrap jQuery collections with [0] or .get(0) before calling $.contains(container, contained).
No. Only element nodes are supported for the second argument. Text and comment nodes return false even when they appear inside the container.
Native Node.contains() accepts any Node type, including text nodes. jQuery.contains() is stricter — elements only for the second argument — and was added for consistent cross-browser checks in jQuery apps.
contains() is a boolean test between two known element nodes. .find() searches descendants from a jQuery collection. .has() filters a collection to elements that contain a matching descendant.
No. It needs a browser DOM. Use it in the browser with jQuery loaded; on the server, use a DOM library or skip this helper.

Next: data()

Store and read custom data on DOM elements with jQuery.

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