jQuery — Descendants
children()
$("ul").children(); // All direct children
$("ul").children(".active"); // Only matching
find()
$("ul").find("li"); // All descendant li elements
$("ul").find(".highlight"); // Matching descendants
children() vs find()
// children() — one level deep
$("div").children("p"); // Direct child p only
// find() — all levels deep
$("div").find("p"); // All p descendants
contents()
// Get all children including text nodes and iframes
$("p").contents();
Practical: Navigation
// Initialize accordion
$(".accordion-header").click(function() {
$(this)
.next(".accordion-content")
.slideToggle(300)
.siblings(".accordion-content")
.slideUp(300);
});
first() and last()
$("ul").children().first();
$("ul").children().last();
Traversal Summary
| Method | Direction | Depth |
|---|---|---|
| children() | Down | One level |
| find() | Down | All levels |
| contents() | Down | Includes text nodes |
Mini Practice
- Select all direct children of a list
- Use find() to get all nested elements
- Use first() and last() on children
- Build a navigation tree
Up Next
Continue with Siblings — navigating between sibling elements.
Related Topics
Frequently Asked Questions about Descendants
What is Descendants in jQuery?
Descendants is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Descendants?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Descendants.
Why is Descendants important in jQuery?
Descendants is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.