jQuery .has() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.has(selector|element) (since jQuery 1.4) reduces the matched set to elements that contain a matching descendant. The result is the containers — not the inner matches.

Syntax

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

jQuery
// Official pattern — li that contain a nested ul
$("li").has("ul");

// Yes/No: does the ul contain any li?
$("ul").has("li").length ? "Yes" : "No";

// Same idea as :has() at query time
$("li:has(ul)");

// Not the same as .find() (returns descendants)
$("li").find("ul");
CallWhat it does
Matched setCandidate containers
selectorDescendant pattern to look for
elementSpecific DOM node that must be inside
DepthAny depth (not just direct children)
Return valueNew jQuery set (subset of parents)

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

Parameters

ParameterDescription
Step 1Select the candidate containers — e.g. $("li") or $("div.card").
Step 2Call .has("selector") or .has(domElement).
Step 3Remember: the result is a subset of the original set.

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 $("li").has("ul")

Mark the list item that contains a nested list (CSS-free).

jQuery
$("li").has("ul").append(" [has ul]");
Try It Yourself

How It Works

  1. Candidates. All li elements are considered.
  2. Test. Keep only those with a descendant ul.
  3. Mark. Append runs on the container li.

Example 2 — Does the ul Contain an li?

Official Ex. idea: report Yes/No from .has().length.

jQuery
$("#out").text(
  "Does the UL contain an LI? " +
  ($("ul.demo").has("li").length ? "Yes" : "No")
);
Try It Yourself

How It Works

  1. Container. Start from ul.demo.
  2. Has. Keep it only if an li descendant exists.
  3. Length. Non-zero means Yes.

Example 3 — .has() vs .find()

Same list — containers vs the nested lists themselves.

jQuery
$("#out").text(
  "has('ul') → " + $("li").has("ul").length +
  " | find('ul') → " + $("li").find("ul").length
);
Try It Yourself

How It Works

  1. has. Returns the parent li that wraps a ul.
  2. find. Returns the nested ul element(s).
  3. Counts. May match in size but point at different nodes.

Frequently Asked Questions

.has() reduces the current jQuery collection to elements that contain at least one descendant matching a selector or DOM element. It returns the container elements — not the matching descendants themselves.
.find() returns the descendant elements that match. .has() returns the ancestors from the current set that contain such descendants. $("li").find("ul") gives nested uls; $("li").has("ul") gives li elements that wrap a ul.
.filter() tests each element in the current set — the element itself must match. .has() tests descendants inside each element — the parent stays if any descendant matches.
Yes, since jQuery 1.4. .has(domElement) keeps elements from the current set that contain that DOM node as a descendant.
No. .has() checks all descendants at any depth — grandchildren and deeper included.
Both mean “elements that have X inside.” $("div:has(p)") does it at query time; $("div").has("p") applies the same idea to an existing collection.

Next: .index() Method — Find Element Position

Find an element's position among its siblings or within a set.

.index() Method — Find Element Position 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