Easy Tutorial
❮ Event Toggle Ajax Serialize ❯

jQuery Traversal - Descendants


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

With jQuery, you can traverse down the DOM tree to find elements' descendants.


Traversing Down the DOM Tree

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


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();
});

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

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

Example

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

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");
});

The following example returns all descendants of <div>:

Example

$(document).ready(function(){
  $("div").find("*");
});
❮ Event Toggle Ajax Serialize ❯