jQuery Traversing -Filtering


Narrow down the search range of elements

The three most basic filtering methods are: first(), last(), and eq(), which allow you to select a specific element based on its position in a group 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 <p> element inside the first <div> element:

Example

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

Try it yourself »


jQuery last() Method

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

The following example selects the last <p> element in the last <div> element:

Example

$(document).ready(function(){ $("div p").last(); });

Try it yourself »


jQuery eq() Method

The eq() method returns an element with a specified index number from the selected elements.

Index numbers start at 0, so the index number of the first element is 0, not 1. The following example selects the second <p> element (index number 1):

Example

$(document).ready(function(){ $("p").eq(1); });

Try it yourself »


jQuery filter() Method

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

The following example returns all <p> elements with the class name "url":

Example

$(document).ready(function(){ $("p").filter(".url"); });

Try it yourself »


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 <p> elements that do not have the class name "url":

Example

$(document).ready(function(){ $("p").not(".url"); });

Try it yourself »
Other Extensions