HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
The 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
The 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, like filter()
and not()
allow you to select elements
that match, or do not match, a certain criteria.
The first()
method returns the first element of the specified elements.
The following example selects the first <div>
element:
$(document).ready(function(){
$("div").first();
});
The last()
method returns the last element of the specified elements.
The following example selects the last <div>
element:
$(document).ready(function(){
$("div").last();
});
The eq()
method returns an element with a specific index number of the
selected elements.
The index numbers start at 0, so the first element will have the index number
0 and not 1. The following example selects the second <p>
element (index number
1):
$(document).ready(function(){
$("p").eq(1);
});
The filter()
method lets you specify a criteria. Elements that do not match
the criteria are removed from the selection, and those that match will be
returned.
The following example returns all <p>
elements with class name "intro":
$(document).ready(function(){
$("p").filter(".intro");
});
The not()
method returns all elements that do not match the criteria.
not()
method is the opposite of filter()
.
The following example returns all <p>
elements that do not have class name
"intro":
$(document).ready(function(){
$("p").not(".intro");
});
Use a jQuery method to get the first <div> element in the document.
$("div").();
For a complete overview of all jQuery Traversing methods, please go to our jQuery Traversing Reference.