jQuery Traversing - Filtering


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

The simplest ways to filter elements are using first(), last(), and eq(). These methods help you pick out a particular element from a group of elements based on where it is positioned.

Different ways to filter elements are available. For example, you can use the filter() and not() methods to choose elements that either meet or don't meet specific criteria. These methods help you select the elements you want based on your conditions.


jQuery first() Method

The first() function gives you the initial item from a group of chosen elements.

This example chooses the initial <div> element:


jQuery last() Method

The last() function gives you the final item from the provided elements.

Here's an example that picks the final <div> element:


jQuery eq() method

The eq() function is used to pick out a specific element from a group of elements. It does this by using a position number, which starts from 0, not 1. For instance, if you want to select the second <p> element in a list, you would use an index number of 1.


jQuery filter() Method

The filter() method helps you choose specific things. It takes away the ones that don't fit your choice and gives you the ones that do.

The next example fetches all <p> elements having the class name "intro":


jQuery not() Method

The not() function gives you all the elements that don't fit the specified conditions.

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

The following example retrieves all <p> elements that lack the "intro" class attribute: