The .children() traversing method collects direct child elements of each matched parent — one level down the DOM tree, optionally filtered by a selector. This tutorial follows the official jQuery nested-list demo, click-to-highlight children pattern, and compares .children() with .find() and .contents().
01
Syntax
.children()
02
One level
Direct only
03
Selector
Optional filter
04
vs find
All depths
05
Elements
Not text nodes
06
Since 1.0
Core API
Fundamentals
Introduction
Parent elements often contain a mix of direct children — list items inside a <ul>, paragraphs and spans inside a <div>, table rows inside a <tbody>. When you need to target those immediate offspring without grabbing deeper nested nodes, jQuery provides .children().
Available since jQuery 1.0, .children() walks exactly one level down from each element in the current set and builds a new jQuery object from the matching child elements. Pass an optional selector to keep only children that match a CSS expression.
Concept
Understanding the .children() Method
Given a jQuery object representing one or more DOM elements, .children([selector]) searches the direct children of each element and returns them as a new jQuery collection. Grandchildren and deeper descendants are excluded unless you use .find() instead.
Like most jQuery methods, .children() returns only element nodes — not text nodes or comments. To include text and comment nodes, use .contents(). The method does not modify the DOM; it only queries what is already on the page.
💡
Beginner Tip
Think of .children() as looking at the first row of boxes inside a container. .find() opens every nested box inside too.
Foundation
📝 Syntax
General form of .children:
jQuery
.children( [selector ] )
Parameters
selector (optional) — a string containing a selector expression. When provided, only direct children matching the selector are included in the result.
Return value
A new jQuery object containing the direct child elements of each matched parent, optionally filtered.
Official jQuery API nested-list example
jQuery
$( "ul.level-2" ).children().css( "background-color", "red" );
// Direct li children A, B, C → red
// Nested li 1, 2, 3 inside B are NOT selected
Cheat Sheet
⚡ Quick Reference
Goal
Code
All direct children
$("div").children()
Filtered children
$("div").children(".selected")
List item children
$("ul").children("li")
All descendants (any depth)
$("ul").find("li")
Include text nodes
$("div").contents()
Count direct children
$("div").children().length
Compare
📋 .children() vs .find() vs .contents()
Three downward traversal methods — different depth and node types.
.children()
1 level
Direct child elements only
.find()
any depth
All descendant elements
.contents()
+ text
Elements, text, comments
Selector
optional
All three accept filters
Hands-On
Examples Gallery
Examples 1–3 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demos for one-level child selection.
Example 1 — Official Demo: Nested List (One Level Only)
From the jQuery API: highlight direct li children of ul.level-2 — items A, B, and C — not the nested 1, 2, 3 inside B.
li A, B, C → red background
Nested li 1, 2, 3 under B → unchanged
How It Works
.children() stops at the first DOM level. The nested ul inside B is a child, but the li elements inside that nested list are grandchildren — excluded from this query.
📈 Practical Patterns
Style all direct children, filter by class, interactive clicks, and depth comparison.
Example 2 — Official Demo: All Direct Children of Each div
Apply a double red border-bottom to every direct child of every div on the page — official Example 2.
Click a div → its direct child elements highlight.
Console logs count and tag name of clicked element.
How It Works
event.target is wrapped with $(), then .children() collects only immediate offspring. Clicking a leaf node with no element children returns an empty set (length 0).
Example 5 — .children() vs .find() on the Same List
See how depth changes the result on a nested ul structure.
jQuery
var withChildren = $( "ul.level-2" ).children( "li" ).length;
var withFind = $( "ul.level-2" ).find( "li" ).length;
console.log( "children:", withChildren ); // 3 — A, B, C only
console.log( "find:", withFind ); // 6 — includes nested 1, 2, 3
This is the key distinction from the jQuery API docs: .children() travels one level; .find() walks the entire subtree. Choose based on how deep you need to search.
Applications
🚀 Common Use Cases
Navigation menus — select top-level li items with $("ul.menu").children("li").
Table rows — target direct tr children of tbody without nested tables.
Form field groups — style immediate inputs inside a fieldset container.
Accordion panels — toggle direct panel headers without affecting nested content wrappers.
Interactive exploration — highlight direct children on click for DOM debugging.
Filtered child actions — bind events to .children("button") only.
🧠 How .children() Walks the DOM
1
Start with parents
Current jQuery set holds one or more parent elements.
Input
2
Read childNodes
jQuery inspects direct children — element nodes only.
One level
3
Apply selector
Optional filter keeps only children matching the CSS expression.
Filter
4
🖼
Return collection
Chain .css(), .on(), or .hide() on child elements.
Important
📝 Notes
Available since jQuery 1.0; works in all jQuery 1.x, 2.x, and 3.x versions.
Travels exactly one DOM level — not grandchildren or deeper descendants.
Does not return text nodes — use .contents() for text and comments.
Empty parents return an empty jQuery object — safe to chain without errors.
Selector is optional; omit it to get all direct child elements.
For all depths, use .find(selector) instead of .children(selector).
Compatibility
Browser Support
.children() has been part of jQuery since 1.0+. It relies on standard DOM child-element traversal and works wherever jQuery runs.
✓ jQuery 1.0+
jQuery .children()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: element.children (HTMLCollection) or querySelector(':scope > selector').
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.children()Universal
Bottom line: Safe in any jQuery project. Prefer .children() for one level; use .find() when you need descendants at any depth.
Wrap Up
Conclusion
The jQuery .children() method selects direct child elements one level down from each matched parent — optionally filtered by a CSS selector. It is the right tool when you need immediate offspring without diving into nested subtrees.
Remember the official nested-list lesson: .children() highlights A, B, and C but not the nested items inside B. When you need every descendant at any depth, reach for .find() instead.
Use .children() for top-level menu items or table rows
Pass a selector to narrow direct children: .children("li")
Compare counts with .find() when depth is unclear
Cache parent references when querying children repeatedly
Use .contents() when text nodes matter
❌ Don’t
Expect .children() to reach grandchildren
Confuse .children() with .child() — no such method
Use .children() when you need all nested li elements
Assume text nodes are included — they are not
Mutate the DOM expecting .children() to traverse changes mid-chain incorrectly
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .children()
Direct offspring, one level down.
5
Core concepts
🖼01
.children()
Direct kids
API
102
One level
Not deeper
Depth
🔍03
Selector
Filter kids
Arg
↓04
.find()
All depths
Compare
📄05
Elements
Not text
Nodes
❓ Frequently Asked Questions
.children() returns a new jQuery object containing the direct child elements of each element in the current set — one level down the DOM tree only. An optional selector filters which child elements are included.
.children() only selects immediate children (one DOM level). .find() searches all descendants at any depth — grandchildren, great-grandchildren, and deeper. Use .children() when you only want direct offspring; use .find() to drill down the whole subtree.
.children() returns only element nodes (tags like li, p, div). .contents() includes text nodes, comment nodes, and elements — everything directly inside the parent. Use .contents() when you need raw text nodes or mixed node types.
No. Nested elements inside a child are not selected — only direct children of each matched parent. In a nested ul, $('ul').children() returns the li elements directly under that ul, not li inside nested ul elements.
.children(selector) filters direct children so only those matching the CSS selector are returned — for example, $('div').children('.selected') keeps only direct children with class selected.
No. Like all jQuery traversing methods, .children() only queries the existing DOM tree and returns a new jQuery collection. It does not add, remove, or move elements.
Did you know?
The jQuery API nested-list demo is the classic way to teach .children() vs .find(): items A, B, and C are direct children of ul.level-2, but items 1, 2, and 3 sit inside a nested ul under B. .children() stops at A, B, C; .find("li") returns all six. Once you picture that tree, the one-level rule clicks.