Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

jQuery children() Method

❮ jQuery Traversing Methods

Example

Return elements that are direct children of <ul>:

$(document).ready(function(){
  $("ul").children().css({"color": "red", "border": "2px solid red"});
});

Result:

body (great-grandparent)
div (grandparent)
    ul (parent)
  • li (child) span (grandchild)
Try it Yourself »

Definition and Usage

The children() method returns all direct children of the selected element.

The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return 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's root element (to return parents or other ancestors), use the parent() or parents() method.

Note: This method does not return text nodes. To return all children including text nodes, use the contents() method.


Syntax

$(selector).children(filter)

Parameter Description
filter Optional. Specifies a selector expression to narrow down the search for children

Try it Yourself - Examples

Return all direct children of <ul>
How to return all direct children of an <ul> element.

Narrow down the search
How to use the filter parameter to return all <li> elements with the class name "first", that are direct children of <ul>.

Return all <p> elements that are direct children of <div>
How to select all <p> elements that are direct children of their parent <div> element.

Show the descendants of an element by tag names
A demonstration which shows who the descendants of a <div> element actually are.


❮ jQuery Traversing Methods

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.