jQuery - filter( fn ) Method

The filter( fn ) method filters all elements from the set of matched elements that do not match the specified function.
Here is the simple syntax to use this method −
selector.filter( selector )

Here is the description of all the parameters used by this method −
fn − The function is called with a context equal to the current element just like $.each. If the function returns false, then the element is removed otherwise the element is kept.
fn − The function is called with a context equal to the current element just like $.each. If the function returns false, then the element is removed otherwise the element is kept.
Following is an example showing a simple usage of this method −
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("li").filter(function (index) {
               return index == 1 || $(this).attr("class") == "middle";
            }).addClass("selected");
         });
      </script>
		
      <style>
         .selected { color:red; }
      </style>
   </head>
	
   <body>
      <div>
         <ul>
            <li class = "top">list item 1</li>
            <li class = "top">list item 2</li>
            <li class = "middle">list item 3</li>
            <li class = "middle">list item 4</li>
            <li class = "bottom">list item 5</li>
            <li class = "bottom">list item 6</li>
         </ul>
      </div>
   </body>
</html>
This will produce following result −
list item 1
list item 2
list item 3
list item 4
list item 5
list item 6
Following is an example showing a simple usage of this method −
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("li").filter(function (index) {
               return index == 1 || $(this).attr("class") == "middle";
            }).addClass("selected");
         });
      </script>
		
      <style>
         .selected { color:red; }
      </style>
   </head>
	
   <body>
      <div>
         <ul>
            <li class = "top">list item 1</li>
            <li class = "selected">list item 2</li>
            <li class = "selected">list item 3</li>
            <li class = "selected">list item 4</li>
            <li class = "bottom">list item 5</li>
            <li class = "bottom">list item 6</li>
         </ul>
      </div>
   </body>
</html>
This will produce following result −
list item 1
list item 2
list item 3
list item 4
list item 5
list item 6

    27 Lectures 
     1 hours 

 Mahesh Kumar

    27 Lectures 
     1.5 hours 

 Pratik Singh

    72 Lectures 
     4.5 hours 

 Frahaan Hussain

    60 Lectures 
     9 hours 

 Eduonix Learning Solutions

    17 Lectures 
     2 hours 

 Sandip Bhattacharya

    12 Lectures 
     53 mins

 Laurence Svekis
 Print
 Add Notes
 Bookmark this page