jQuery Child Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

The child combinator parent > child (since jQuery 1.0) selects only direct children of a parent. It is a more specific form of the descendant combinator (parent child), which matches at any nesting depth. Official docs call it a standard CSS combinator.

Syntax

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

jQuery
$("ul.topnav > li")   // direct li only
$("ul.topnav li")     // every li inside (any depth)

$("div > span")       // spans that are immediate children
$("nav > ul > li > a") // chain one level at a time

// Equivalent traversal: $(parent).children(child)
CallWhat it does
parentAny valid selector for the parent elements
>Child combinator (direct children only)
childFilter applied to each parent’s immediate children
Return valuejQuery collection of matching child elements

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

Parameters

ParameterDescription
Step 1Write a parent selector (id, class, or tag) on the left of >.
Step 2Write a child selector on the right — only matching immediate children are kept.
Step 3Chain more > steps when you need a fixed depth (e.g. nav > ul > li > a).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official ul.topnav > li

Mark only top-level list items — nested sub-list items are skipped.

jQuery
var labels = [];
$("ul.topnav > li").each(function () {
  var text = $.trim(
    $(this).contents().filter(function () {
      return this.nodeType === 3;
    }).text()
  );
  labels.push(text || "Item");
});

$("#out").text(
  "Direct: " + labels.join(" | ") + "\n" +
  "Skipped nested count: " + ($("ul.topnav li").length - labels.length)
);
Try It Yourself

How It Works

  1. Parent. ul.topnav is the menu container.
  2. Combinator. > keeps only that ul’s immediate li children.
  3. Skip. Nested li elements under the inner ul do not match.

Example 2 — Child vs Descendant Count

Same markup, two selectors — see how depth changes the match count.

jQuery
var direct = $("ul.topnav > li").length;
var all = $("ul.topnav li").length;

$("#out").text(
  "Direct children (>): " + direct + "\n" +
  "All descendants (space): " + all
);
Try It Yourself

How It Works

  1. Direct. > counts the three top-level li elements.
  2. Descendant. A space also includes the three nested items → 6.
  3. Rule. Use > when nesting must not inflate your set.

Example 3 — div > span Only

Select spans that sit directly under #content, not inside wrappers.

jQuery
$("#content > span").each(function () {
  $(this).text($(this).text() + " (direct)");
});

$("#out").text(
  "Direct spans: " + $("#content > span").length +
  " | All spans: " + $("#content span").length
);
Try It Yourself

How It Works

  1. Structure. One span is a child of #content; another sits inside #inner.
  2. Filter. #content > span keeps only the direct span.
  3. Label. We append (direct) so you can see which matched.

Frequently Asked Questions

The child combinator (parent > child) selects only direct children of a parent. $("ul.topnav > li") matches top-level list items inside ul.topnav, but not li elements nested inside a sub-list.
A space is the descendant combinator — it matches at any nesting depth. The > combinator matches only first-level children. $("div span") finds every span inside div; $("div > span") finds only spans that are immediate children of div.
Yes. $("nav > ul > li > a") walks one level at a time — a direct ul under nav, that ul’s direct li children, and each li’s direct a children. Use this when you need a precise depth.
Both target direct children. $("parent > child") is a CSS selector string for one-shot queries. .children("child") is a traversal method — useful when you already have a jQuery object and want its immediate children.
Yes. The > combinator is standard CSS (Selectors Level 3) and has been in jQuery since 1.0. Prefer specific parents like $("ul.topnav > li") over overly broad queries.
Use > when nested markup could cause false matches — menus with sub-menus, tables inside tables, or cards with inner wrappers. Direct-child selection keeps the query precise.

Next: :first-child Selector

Learn how to match the first child among siblings with :first-child.

:first-child 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.

8 people found this page helpful