JQuery Filter Selector Example

This tutorial explains usage of the filter selector in jQuery. The filter selector is useful feature to filter all the elements from the set of matched elements that matches the selector or certain criteria given in the function’s test. It returns the elements that match only the filter selector. The elements which do not match the given criteria are removed from the selection and represents only matched elements. The supplied selector is used with each element and those elements that match the specified selector will be represented in the result. The filter selector is opposite of not selector.

JQuery Filter Selector Syntax


It has parameter called criteria is a selector expression which specifies elements to be selected from the set of matched elements.

JQuery Filter Selector Example

<!doctype html>
<title>JQuery Filter Selector</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<h3>JQuery Filter Selector Example</h3>
<script type="text/javascript">
         $("div").filter("#myval").css("border","3px solid red");
         return $("span",this).length==2;}).css("background-color","lightpink");
      $( "span" ).filter( ":middle" ).css( "border", "3px solid green");
<body style="line-height:1.2em">
   <div id="myval">This is First div.</div>
   <div id="myval1">This is Second div.</div>
   <div id="myval">This is Third div.</div>
   <button id="mybutton">Click to see filtered elements</button><br>
<legend><strong>Using function index</strong></legend>
      <span>Hello World!!!.</span>
      Welcome to jQuery.
      <span>It is <span>JavaScript</span> library.</span>
      <span>It is lightweight,"write less, do more",JavaScript library.   </span>
      <span>It is combination of <span>HTML, CSS</span> and JavaScript</span>
<legend><strong>Returns all the elements that are even</strong></legend>
   <p>Hello World!!! 0</p>
   <p>Welcome to jQuery 1</p>
   <p>It is JavaScript library 2</p>
   <p>It is lightweight, "write less, do more", JavaScript library 3</p>
   <p>The purpose of jQuery is to make it much easier to use JavaScript on your website 4</p>
   <p>It is combination of HTML, CSS and JavaScript 5</p>
<legend><strong>Selecting middle elements</strong></legend>
   <span>My First Span</span>&nbsp;
   <span >My Second Span</span>&nbsp;
   <span >My Third Span</span>&nbsp;
   <span>My Fourth Span</span>&nbsp;
  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“#mybutton”).click(function()) line defines click method which occurs when an element is clicked.
  • $(“div”).filter(“#myval”).css(“border”,”3px solid red”); line defines filter selector that filters all the elements from the set of matched elements that do not match specified selector. Here, it matches the elements those have specified idmyval and when we click on the button, it represent filtered elements with red color border.
  • $(“div”).filter(function(index)) line allows to filter against a function rather than a selector. If function returns true, then element will be included in filter set or else it will be excluded.
  • The next line contains two span elements, which refers to each element in the document. It will display the elements with lightpink color which contains exact two span elements.
  • $(“p”).filter(“:even”).css(“background-color”,”orange”); line filter the elements which are of type even. The resulting elements will get display with orange color background.
  • $( “span” ).filter( “:middle” ).css( “border”, “3px solid green”); line defines span elements which contains with green color border. It adds the border to middle elements.

JQuery Filter Selector Demo

When you run the above example, you would get the following output:

JQuery Filter Selector Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!