Introduction to jQuery
JQuery Syntax
JQuery Effects
JQuery HTML
JQuery Traversing
jQuery Traversing Descendants, which is all about moving down the DOM tree from a selected element.
In jQuery, descendant traversal means selecting elements that are nested inside another element — children, grandchildren, great-grandchildren, and so on.
Two useful jQuery methods for traversing down the DOM tree are:
|
Method |
Description |
|---|---|
|
.children() |
Selects immediate child elements only |
|
.find() |
Selects all descendants that match a selector |
The .children() method returns only the direct children of the selected element(s). It does not go deeper like .find().
This method only traverses a single level down the DOM tree.
The following example returns all elements that are direct children of each <div> elements:
Try it yourself
When using jQuery’s .children() method, you can also pass a selector as an optional parameter to filter which child elements you want to select.
The following example returns all <p> elements with the class name "first", that are direct children of <div>:
Try it yourself
The .find() method searches downward in the DOM tree and returns all descendant elements of the selected element that match the given selector — no matter how deeply nested.
The following example returns all <span> elements that are descendants of <div>:
Try it yourself
The following example returns all descendants of <div>:
Try it yourself