jQuery Traversing - Filtering
The first(), last(), eq(), filter() and not() Methods
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.
jQuery first() Method
The first()
method returns the first element of the specified elements.
The following example selects the first <div>
element:
Example
$(document).ready(function(){ $("div").first(); });
jQuery last() Method
The last()
method returns the last element of the specified elements.
The following example selects the last
element:<div>
Example
$(document).ready(function(){ $("div").last(); });
jQuery eq() method
The eq()
method returns an element with a specific index number of the selected elements.
The following example returns all
elements with class name "intro":
Example
$(document).ready(function(){ $("p").filter(".intro"); });
jQuery not() Method
The not()
method returns all elements that do not match the criteria.
The following example returns all
elements that do not have class name "intro":
Example
$(document).ready(function(){ $("p").not(".intro"); });
jQuery last() Method
The last()
method returns the last element of the specified elements.
The following example selects the last
element:<div>
Example
$(document).ready(function(){ $("div").last(); });