HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

jQuery Traversing - Descendants

With jQuery you can traverse down the DOM tree to find descendants of an element.

A descendant is a child, grandchild, great-grandchild, and so on.

Traversing Down the DOM Tree

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

jQuery children() Method

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

This method only traverses a single level down the DOM tree.

The following example returns all elements that are direct children of each <div> elements:

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

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

The following example returns all

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 descendant elements of the selected element, all the way down to the last descendant.

The following example returns all 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("*");
});