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.
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
Parameter
Description
Step 1
Pick the container element (ancestor candidate).
Step 2
Pick the contained element (possible descendant).
Step 3
If you have jQuery objects, unwrap with [0].
Return Value
Return Value
Description
Boolean
Often true or false depending on the match or state.
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Siblings.box-a and box-b share a parent but neither contains the other.
Self. An element is not considered a descendant of itself → false.
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.