Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery Traversal – Descendants

jQuery Traversing - Descendants


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

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


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 children of the selected element.

This method only traverses one level down the DOM tree.

The following example returns the direct parent of each

all direct children of the ... element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

div

"

)

.

children

(

)

;

}

)

;

Try it »

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

The following example returns all ... with class name "1"

elements, and they are

direct children of the ...:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

div

"

)

.

children

(

"

p.1

"

)

;

}

)

;

Try it »


jQuery find() method

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

The following example returns all ... belonging to

descendants of the ...: element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

div

"

)

.

find

(

"

span

"

)

;

}

)

;

Try it »

The following example returns all

descendants of the ...:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

div

"

)

.

find

(

"

"

)

;

}

)

;

Try it »