jQuery Traversing - Filtering


The first(), last(), eq(), filter() and not() Methods

Basic filtering methods are first(), last() and eq(), which allow you to select a specific element based on its location in the group of objects.

Some sorting methods, such as filter() and not(), allow you to select elements that are similar to, or do not match, certain conditions.


jQuery first() Method

The first() method returns the first part of the specified elements.

The following example selects the first <div> item:


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


jQuery last() Method

The last() method returns the last part of the specified elements.

The following example selects the last part of <div>:


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


jQuery eq() method

The eq() method returns an item with a specific index number of selected items.

Index numbers start at 0, so the first item will have a reference number 0 and not 1. The following example selects the second element of <p> (reference number 1):


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


jQuery filter() Method

The filter() method allows you to specify terms. Items that do not match the terms are removed from the selection, and the same ones will be returned.

The following example returns all <p> features with class name "intro":


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


jQuery not() Method

The not() method returns all non-standard features.

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

The following example returns all <p> features that do not have the class name "intro":


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