Home >>jQuery Tutorial >jQuery filter() Method

jQuery filter() Method

jQuery filter() Method

jQuery filter() method in jQuery is used to returns elements that match a certain criteria from the selection, and those that match will be returned.

Syntax:
$(selector).filter(criteria,function(index))

Parameter Values

Parameter Description
Criteria It is an Optional parameter and is used to specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements.
function(index) It is an Optional parameter and is used to specifies a function to run for each element in the set.
  • index – Used to the index position of the element in the set
Here is an Example of jQuery filter() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").filter(".txt1").css("background-color", "#df65f7");
});
</script>
</head>
<body>
<h1>Welcome to Phptpoint</h1>
<p>This is First Paragraph</p>
<p class="txt1">This is Second Paragraph</p>
<p class="txt1">This is Third Paragraph</p>
<p>This is Fourth Paragraph</p>
</body>
</html>

Output:

Welcome to Phptpoint

This is First Paragraph

This is Second Paragraph

This is Third Paragraph

This is Fourth Paragraph