jQuery contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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

Syntax

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

jQuery
$.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])
CallWhat it does
contains()Learn jQuery.contains(): check if a DOM element is a descendant of another, official documentElement demo, nested trees, unwrap jQuery objects, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Pick the container element (ancestor candidate).
Step 2Pick the contained element (possible descendant).
Step 3If you have jQuery objects, unwrap with [0].

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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: jQuery.find() Static Method

Meet the static selector-engine helper that returns a plain array of elements.

jQuery.find() Static Method 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