jQuery Traversing – Filtering | Narrow Down The Search For Elements |jQuery first() Method | jQuery last() Method |jQuery eq() method |jQuery filter() Method |Examples

Narrow Down The Search For Elements :

  • The three 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 selected elements.
  • The following example selects the first <p> element inside the first <div> element:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p").first().css("background-color", "yellow");
});
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>This is the first paragraph in body.</p>

<div style="border: 1px solid black;">
<p>This is the first paragraph in a div.</p>
<p>This is the last paragraph in a div.</p>
</div><br>

<div style="border: 1px solid black;">
<p>This is the first paragraph in another div.</p>
<p>This is the last paragraph in another div.</p>
</div>

<p>This is the last paragraph in body.</p>

</body>
</html>

jQuery last() Method :

  • The last() method returns the last element of the selected elements.
  • The following example selects the last <p> element inside the last <div> element:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p").last().css("background-color", "yellow");
});
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>This is the first paragraph in body.</p>

<div style="border: 1px solid black;">
<p>This is the first paragraph in a div.</p>
<p>This is the last paragraph in a div.</p>
</div><br>

<div style="border: 1px solid black;">
<p>This is the first paragraph in another div.</p>
<p>This is the last paragraph in another div.</p>
</div>

<p>This is the last paragraph in body.</p>

</body>
</html>

jQuery eq() method :

  • 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):

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").eq(1).css("background-color", "yellow");
});
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>My name is Donald (index 0).</p>
<p>Donald Duck (index 1).</p>
<p>I live in Duckburg (index 2).</p>
<p>My best friend is Mickey (index 3).</p>

</body>
</html>

jQuery filter() Method :

  • 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”:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").filter(".intro").css("background-color", "yellow");
});
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>My name is Donald.</p>
<p class="intro">I live in Duckburg.</p>
<p class="intro">I love Duckburg.</p>
<p>My best friend is Mickey.</p>

</body>
</html>

jQuery not() Method :

  • The not() method returns all elements that do not match the criteria.
  • Tip: The not() method is the opposite of filter().
  • The following example returns all <p> elements that do not have class name “intro”:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").not(".intro").css("background-color", "yellow");
});
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p>My name is Donald.</p>
<p class="intro">I live in Duckburg.</p>
<p class="intro">I love Duckburg.</p>
<p>My best friend is Mickey.</p>

</body>
</html>

Example : 1

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// add code here
});
</script>
</head>
<body>

<p>First paragraph.</p>
<p>Second paragraph.</p>
<p>Third paragraph.</p>
<p>Last paragraph.</p>

</body>
</html>

Example : 2

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// add code here
});
</script>
</head>
<body>

<p>First paragraph.</p>
<p>Second paragraph.</p>
<p>Third paragraph.</p>
<p>Last paragraph.</p>

</body>
</html>

Example : 3

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// add code here
});
</script>
</head>
<body>

<p>First paragraph.</p>
<p class="intro">Second paragraph.</p>
<p class="intro">Third paragraph.</p>
<p>Last paragraph.</p>

</body>
</html>

Example : 4

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// add code here
});
</script>
</head>
<body>

<div style="border: 1px solid black;">
<p>This is the first paragraph in a div.</p>
<p>This is the last paragraph in a div.</p>
</div><br>

<div style="border: 1px solid black;">
<p>This is the first paragraph in another div.</p>
<p>This is the last paragraph in another div.</p>
</div>

</body>
</html>