What's the difference between selector and filter() in jQuery? Last Updated : 12 Apr, 2021 Comments Improve Suggest changes Like Article Like Report jQuery Selectors: allow us to select and manipulate HTML element(s). It is used to select HTML elements such as ids, classes, types, attributes, etc using jQuery selectors and then add any CSS properties or events to the selected HTML elements. Syntax: For selecting a button tag the syntax would be $("button") Example: HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> $(document).ready(function() { $("button").click(function() { $("#heading").css("border", "2px solid red"); }); }); </script> </head> <body> <h2 id="heading">Using jQuery Filter</h2> <button>Click to change color of heading</button> </body> </html> Output: On clicking the button, we will see a red border across the heading. Clicking button creates border across heading jQuery Filter: This method is used to specify criteria for an HTML element. Filter() method returns elements that match certain criteria. Syntax: $(selector).filter(criteria, function(index)) Example: HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <script> $(document).ready(function() { $("p").filter(".active").css("background-color", "red"); }); </script> </head> <body> <h1>Jquery Filter</h1> <p>My name is Donald.</p> <p class="active">Geeks</p> <p class="dead">SkeeG</p> <p class="active">For </p> <p class="dead">roF</p> <p class="active">Geeks</p> </body> </html> Output: filter searches out the active class tags and colors them. Filters active class Major differences between selector and filter() in jQuery: Selector in jQuery filter() n jQueryjQuery selector selects all elements based on the elements name given by you.jQuery filter( ) adds further detail to the selected elements by specifying the criteria of selection.It works independently of filter( ), which means there is no need to use it along with the filter( ) method.It works along with the selector. By combining filters with your selectors, we can work to much high degree of precision. Syntax to use it is as follows: $("button") selects all buttons of the HTML page. Syntax to use it as follows: $(button).filter(criteria, function(index)) selects buttons having criteria and applies function on it. Comment More infoAdvertise with us Next Article What's the difference between selector and filter() in jQuery? F faizamu19 Follow Improve Article Tags : Web Technologies JQuery jQuery-Selectors jQuery-Methods Similar Reads What is the difference between position() and offset() in jQuery ? Both the jQueryUI method the returns the object which contains integer co-ordinate properties of the top and left positions. The positions of top and left coordinates are returned in pixels. Both functions are applied only on visible elements, not on hidden elements. Example: The example gives top a 2 min read What is the difference between eq() and get() methods in jQuery ? In this article, we will discuss all the differences between eq() and get() methods in jQuery. eq() Method: This method is used to locate the selected elements directly and returns an element with a specific index. Syntax: $(selector).eq(index) Example: In this example, we will set the different te 2 min read Difference between ID and Class Selector in jQuery jQuery ID Selector: The #id selector specifies the id for an HTML element to be selected. It should not begin with a number and the id attribute must be unique within a document which means it can be used only one at a time. Syntax: $("#id")id is the element's specific id. Example: The following cod 2 min read Difference between text() and html() method in jQuery Text() Method: This method returns the text content of the selected elements. It overwrites the content of all the matches element while setting the content. Syntax: To return text content:$(selector).text()To set text content:$(selector).text(content)To set the text content using a function:$(selec 2 min read What is the difference between find() and filter() methods in JavaScript ? In this article, we will see the difference between the find() and filter() methods in javascript. JavaScript find() method: The find() method is used to find all the descendant elements of the selected element. It finds the element in the DOM tree by traversing through the root to the leaf. Syntax: 2 min read Like