jQuery Traversing - Descendants
Descendants are children, grandchildren, great-grandchildren, and so on.
With jQuery, you can traverse down the DOM tree to find the descendants of an element.
Traversing down the DOM tree
Here are two jQuery methods for traversing down the DOM tree:
- children()
- find()
jQuery children() method
The children() method returns all direct children of the selected element.
This method only traverses one level down the DOM tree.
The following example returns the direct parent of each
Example
$
(
document
)
.
ready
(
function
(
)
{
$
(
"
div
"
)
.
children
(
)
;
}
)
;
Try it »
You can also use an optional parameter to filter the search for children.
The following example returns all ... with class name "1"
elements, and they are
Example
$
(
document
)
.
ready
(
function
(
)
{
$
(
"
div
"
)
.
children
(
"
p.1
"
)
;
}
)
;
Try it »
jQuery find() method
The find() method returns the descendant elements of the selected element, all the way down to the last descendant.
The following example returns all ... belonging to
Example
$
(
document
)
.
ready
(
function
(
)
{
$
(
"
div
"
)
.
find
(
"
span
"
)
;
}
)
;
Try it »
The following example returns all
Example
$
(
document
)
.
ready
(
function
(
)
{
$
(
"
div
"
)
.
find
(
"
"
)
;
}
)
;
Try it »