Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery traversal - filtering


May 07, 2021 jQuery


Table of contents


jQuery traversal - Filter


Narrow the search element

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

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


jQuery first() method

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

Here's an example of picking the first element inside the first .

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

Try it out . . .


jQuery last() method

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

The following example selects the last one in the last element of the .lt;p>

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

Try it out . . .


jQuery eq() method

The eq() method returns an element in the selected element with the specified index number.

The index number starts at 0, so the index number of the first element is 0 instead of 1. The following example picks the second element (index number 1):

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

Try it out . . .


jQuery filter() method

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

The following example returns all the elements with the class name "intro":

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

Try it out . . .


jQuery not() method

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

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

The following example returns all the elements without the class name "intro":

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

Try it out . . .