← Back to Chapters

jQuery Traversing - Descendants

? jQuery Traversing - Descendants

? Quick Overview

Descendant traversal moves down the DOM tree. jQuery provides methods to select direct children or deeply nested elements.

? Key Concepts

  • children() → direct level only
  • find() → any depth below

? Syntax / Theory

? View Code Example
// Basic descendant traversal syntax
$(selector).children();
$(selector).children(".className");
$(selector).find("tag, .class, #id");

? Example 1: children()

Hierarchy: Wrapper → Row → Cell

Wrapper #1
Row A A1 A2
Row B B1 B2
? View Code Example
// Selecting direct children of wrapper
$('#wrap-1').children();
// Selecting only .row children
$('#wrap-1').children('.row');

? Example 2: find()

Hierarchy: Box → Inner → Deep → Leaf

Box #1
Leaf 1 Leaf 2
Box #2
Leaf 3
? View Code Example
// Finding elements at any depth
$('.box').find('.leaf');
$('#box-1').find('.leaf.special');

? Combining children() ➜ find()

Use children() to narrow scope, then find() to dive deeper.

R1 R2
R3 R4
? View Code Example
// Chain traversal for precision
$('#wrap-3').children('.row').find('.tag').addClass('highlight');

? Live Interactive Sandbox

Type a selector below to see how children() vs find() behaves on the "Master Container".

Master Container
Direct Child (div) Nested Child (span)
Direct Child (div) Nested Child (span)

 

? Live Explanation

  • children() → one level down
  • find() → unlimited depth
  • Chaining gives clarity and control

✅ Tips & Best Practices

  • Use children() for predictable structures
  • Reserve find() for deep searches
  • Avoid find('*') for performance

? Try It Yourself

  • Highlight only second row cells
  • Change leaf text in Box #2
  • Toggle a new class using .toggleClass()