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

jQuery Traversal – Filtering

jQuery Traversal - Filtering


Narrow down the scope of the elements being searched

The three most basic filtering methods are: first(), last(), and eq(), which allow you to select a specific element based on its position within a set of elements.

Other filtering methods, such as filter() and not(), allow you to select elements that match or do not match a specified criterion.


jQuery first() method

The first() method returns the first element of the selected elements.

The following example selects the first

element, the first ... inside it:

element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

div p

"

)

.

first

(

)

;

}

)

;

Try it »


jQuery last() method

The last() method returns the last element of the selected elements.

The following example selects the last

element, the last ... inside it:

element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

div p

"

)

.

last

(

)

;

}

)

;

Try it »


jQuery eq() method

The eq() method returns the element with the specified index number among the selected elements.

Index numbers start at 0, so the first element has index 0, not 1. The following example selects the second

element (index 1):

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

p

"

)

.

eq

(

1

)

;

}

)

;

Try it »


jQuery filter() method

The filter() method allows you to specify a criterion. Elements that do not match this criterion are removed from the set, and matching elements are returned.

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

element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

p

"

)

.

filter

(

"

.url

"

)

;

}

)

;

Try it »


jQuery not() method

The not() method returns all elements that do not match the criterion.

Tip: The not() method is the opposite of filter().

The following example returns all ... without class name "url"

element:

Example

$

(

document

)

.

ready

(

function

(

)

{

$

(

"

p

"

)

.

not

(

"

.url

"

)

;

}

)

;

Try it »