jQuery | filter() with Examples

13 Feb, 2019
The jQuery is a very powerful tool which helps us to incorporate a variety of DOM traversal methods to select elements in a document randomly or in sequential order. Most of the DOM traversal methods do not modify the elements whereas they filter them out upon the given conditions.The filter() method is used to filter out all the elements that do not match the selected criteria and those matches will be returned.Syntax:
$(selector).filter(criteria, function(index))
Parameters:criteria : It specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements.function(index) : It specifies a function to run for each element in the set. If the function returns true, the element is kept. Otherwise, it is removed.index : The index position of the element in the set.NOTE : To specify more than one criteria, use a comma.
<html>  <head>    <title>GEEKS FOR GEEKS ARTICLE</title>    <script src="https://ajax.googleapis.com/ajax/libs/                 jquery/3.3.1/jquery.min.js"></script>    <script type="text/javascript">        $(document).ready(function() {            $("li").filter(".first, .last").css("color", "red")                .css("backgroundColor", "yellow");        });    </script></head>  <body>    <ul>        <li class="first">GeeksForGeeks</li>        <li class="first">GeeksForGeeks</li>        <li class="middle">GeeksForGeeks</li>        <li class="last">GeeksForGeeks</li>        <li class="last">GeeksForGeeks</li>    </ul></body>  </html>
Output:Code #2:This code will select the elements matching with the criteria checked by the function. Here, the function checks for two elements in a list and returns true or false.
<html>  <head>    <title>GEEKS FOR GEEKS ARTICLE</title>    <script src="https://ajax.googleapis.com/ajax/libs/                 jquery/3.3.1/jquery.min.js"></script>    <script type="text/javascript">        $(document).ready(function() {            $("ul").filter(function() {                return $("li", this).length == 2;            }).css("color", "red").css("fontSize", "20");        });    </script></head>  <body>    A list with two elements:    <ul>        <li>option1</li>        <li>option2</li>    </ul>    A list with one element:    <ul>        <li>option1</li>    </ul>    A list with two elements:    <ul>        <li>option1</li>        <li>option2</li>    </ul>    A list with three elements:    <ul>        <li>option1</li>    </ul>    <ul>        <li>option2</li>    </ul>    <ul>        <li>option3</li>    </ul></body>  </html>
Output:
jQuery-Traversing
JavaScript
JQuery
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
Difference Between PUT and PATCH Request
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to add options to a select element using jQuery?
How to fetch data from JSON file and display in HTML table using jQuery ?