Total Page Preview: 000000000299
What is filter() in jQuery?
jQuery filter() method
In jQuery, the filter() method is used to narrow down the set of matched elements in a jQuery object based on specified criteria. It reduces the set of matched elements to those that match the selector or pass a provided function's test.
Here's the basic syntax of the filter() method:
-
selector: A string containing a selector expression to match elements against.
-
filterFunction: A function used as a test for each element in the set. This function should return true to include the element in the filtered set or false to exclude it.
Here's an example using the filter() method with a selector:
In this example, the filter('.fruit') method is used to select only the <li> elements with the class 'fruit'. Then, the addClass('highlight') method adds a 'highlight' class to those filtered elements.
You can also use a function as the argument to filter(). The function will be called for each element in the set, and the elements for which the function returns true will be included in the filtered set. Here's an example:
In this example, the filter() method is used with a function that checks whether the length of the text in each <li> element is greater than 5. If it is, the element is included in the filtered set, and a 'highlight' class is added to it.
Thank You
About Author
Brijesh Kumar
Database Developer
I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL
Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall
and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com