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

jQuery Traversal – Siblings(siblings)

jQuery Traversing - Siblings


Siblings share the same parent element.

With jQuery, you can traverse the sibling elements of an element in the DOM tree.


Traversing horizontally in the DOM tree

There are many useful methods that let us traverse horizontally in the DOM tree:


jQuery siblings() method

The siblings() method returns all sibling elements of the selected element.

The following example returns all

all sibling elements of the ...:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

h2

"

)

.

siblings

(

)

;

}

)

;

Try it »

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

The following example returns all ... belonging to

all ... that are siblings of the ...

element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

h2

"

)

.

siblings

(

"

p

"

)

;

}

)

;

Try it »


jQuery next() method

The next() method returns the next sibling element of the selected element.

This method returns only one element.

The following example returns all

the next sibling element of the ...:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

h2

"

)

.

next

(

)

;

}

)

;

Try it »


jQuery nextAll() method

The nextAll() method returns all following sibling elements of the selected element.

The following example returns all

all following sibling elements of the ...:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

h2

"

)

.

nextAll

(

)

;

}

)

;

Try it »


jQuery nextUntil() method

The nextUntil() method returns all following sibling elements between two given arguments.

The following example returns all ancestor elements between

The difference between

all sibling elements between the ... elements:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

h2

"

)

.

nextUntil

(

"

h6

"

)

;

}

)

;

Try it »


jQuery prev(), prevAll() & prevUntil() methods

The prev(), prevAll(), and prevUntil() methods work similarly to the methods above, except in the opposite direction: they return the preceding sibling elements (traversing along the siblings before the element in the DOM tree, rather than after it).