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
Call
What it does
Preceding selector
Outer candidates to keep or drop
Inner selector
Descendant that must exist somewhere inside
.has() method
Same 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
Parameter
Description
Step 1
Choose the outer elements to test ($("div"), $("li"), …).
Step 2
Decide which descendant must exist (p, ul, .error, …).
: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.