jQuery :has() Selector — Match Parents With Descendants

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:has(selector) (since jQuery 1.1.4) selects elements that contain at least one descendant matching selector — at any nesting depth, not only direct children.

Syntax

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

jQuery
$("div:has(p)");           // divs with a <p> inside
$("li:has(ul)");           // list items with a nested list
$("article:has(.error)");  // articles that hold an error

// Preferred today:
$("div").has("p");         // same idea, faster path
CallWhat it does
Preceding selectorOuter candidates to keep or drop
Inner selectorDescendant that must exist somewhere inside
.has() methodSame filter; better with pure CSS base selects

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

Parameters

ParameterDescription
Step 1Choose the outer elements to test ($("div"), $("li"), …).
Step 2Decide which descendant must exist (p, ul, .error, …).
Step 3Legacy: append :has(selector) · Modern: call .has(selector).

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official div:has(p)

From the jQuery docs: add class test to divs that contain a paragraph.

jQuery
$("div:has(p)").addClass("test");
Try It Yourself

How It Works

  1. Candidates. Every div is tested.
  2. has(p). Keep only those with a p somewhere inside.
  3. Depth. Nested paragraphs still count.

Example 2 — Modern .has() Method

Same result as :has(p) — the recommended performance pattern.

jQuery
$("div").has("p").addClass("test");
// Same matches as $("div:has(p)")
Try It Yourself

How It Works

  1. CSS first. $("div") can use querySelectorAll.
  2. Filter. .has("p") keeps containers with a p.
  3. Same idea. Still returns parents, not the paragraphs.

Example 3 — Nested Lists li:has(ul)

Bold list items that contain a nested sublist.

jQuery
$("li:has(ul)").css("fontWeight", "bold");
// Modern: $("li").has("ul").css("fontWeight", "bold");
Try It Yourself

How It Works

  1. Set. All li elements are candidates.
  2. has(ul). Keep only those wrapping a ul.
  3. Leaves. Items without a sublist are skipped.

Frequently Asked Questions

:has(selector) selects elements that contain at least one descendant matching the inner selector. $("div:has(p)") matches every div with a p anywhere inside — not only as a direct child. Available since jQuery 1.1.4.
No. Official docs: the match can exist anywhere among descendants — grandchildren and deeper count. Use the child combinator (>) when you need direct children only.
Official jQuery docs recommend $("div").has("p") instead of $("div:has(p)") for better performance in modern browsers. Separate a pure CSS base selector from the descendant filter.
They look similar. jQuery :has() is a jQuery extension since 1.1.4 and does not use native querySelectorAll the same way. Modern CSS also defines :has(), but in jQuery code prefer .has() on a CSS-selected collection.
:has() filters by descendant elements matching a selector — structure. :contains() filters by visible text substring — content. div:has(p) looks for a p tag; div:contains("Hello") looks for text.
.find() returns the matching descendants. :has() / .has() return the container elements that hold those descendants. $("div").find("p") gives paragraphs; $("div").has("p") gives the divs.

Next: :header Selector

Select heading elements (h1–h6) in one go.

:header Selector 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.

10 people found this page helpful