jQuery Traversal -Descendants


Descendants are children, grandchildren, great-grandchildren, and so on.

With jQuery, you can traverse down the DOM tree to find an element's descendants.


Traversing Down the DOM Tree

Below are two jQuery methods for traversing down the DOM tree:

  • children()
  • find()

jQuery children() Method

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

This method only traverses one level down the DOM tree.

The following example returns all direct child elements of each <div> element:

Example

$(document).ready(function(){ $("div").children(); });

Try it Yourself »

You can also use an optional parameter to filter the search for child elements.

The following example returns all <p> elements with class name "1" that are direct children of <div>:

Example

$(document).ready(function(){ $("div").children("p.1"); });

Try it Yourself »


jQuery find() Method

The find() method returns descendant elements of the selected element, all the way down to the last descendant.

The following example returns all <span> elements that are descendants of <div>:

Example

$(document).ready(function(){ $("div").find("span"); });

Try it Yourself »

The following example returns all descendants of <div>:

Example

$(document).ready(function(){ $("div").find("*"); });

Try it Yourself »
Other Extensions