jQuery Traversing - Descendants


With jQuery you can drop down a DOM tree to earn interest on something.

A child is a child, a grandchild, a grandchild, and so on.


Traversing Down the DOM Tree

Two useful jQuery methods for traversing down the DOM tree are:

  • children()
  • find()

jQuery children() Method

The children() method returns all children specific to the selected element.

This route exceeds only one level under the DOM tree.

The following example returns all the child-specific features of each <div> element:


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

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

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


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


jQuery find() Method

The find() method returns the interest components of the selected object, all the way down to the last generation.

The following example returns all <span> elements that are <div> derivative features:


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

The following example returns all generations of <div>: You can also use the parameter of your choice to filter child search.


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