jQuery children() method
Example
Return
-
direct children of the ...:
- li (child) span (grandchild)
- .
all of the direct child elements of the
Element
How to selectall direct child elements of the.
Demonstrate descendants of an element by tag name
Demonstratedescendants of the element.
$(document).ready(function(){
$("ul").children().css({"color":"red","border":"2px solid red"});
});
Result:
body (great-grandparent)
div (grandparent)
Try it »
Definition and Usage
The children() method returns all direct children of the selected element.
**DOM Tree:** This method only traverses a single level down the DOM tree. To traverse multiple levels down (returning children, grandchildren, or other descendants), use the find() method.
**Tip:** To traverse a single level up the DOM tree, or all the way up to the document root element (returning the parent node or other ancestors), use parent() or parents() method.
**Note:** This method does not return text nodes. To return all child nodes including text nodes, use contents() method.
Syntax
$(
selector
).children(
filter
)
| Parameters | Description |
|---|---|
| filter | Optional. Specifies a selector expression to narrow down the search for child elements. |
More examples
-
all direct child elements of the
How to return
-
all direct child elements of the element.
Narrow down the search
How to use the filter parameter to return
-
all with class name "1" among the direct child elements of the
jQuery Traversal Methods