jQuery has(), not() and filter() functions example

jQuery API provides traversing methods to filter out the matched elements. We will discuss some filtering methods in this post, jQuery has(), not() and filter().

jQuery has()

The has() method returns all the elements which matches at least one of the elements passed to this method. The has() method accepts a mandatory parameter which reduces the matched set of elements..

Here is the general syntax for using has():

element could be any selector expression to match against or any DOM element. You can pass multiple comma separated elements this methods like $(“div”).has(“p,span”). Here has() method return all the div elements which contains any of the elements, that is p or span element.

jQuery has() example

Following example demonstrates the jQuery has() usage.

In this example, we have two div elements. We are going to check whether the div elements have a p element using jQuery has().
$("div").has("p") checks whether the div has a p element. The has() method traverses the DOM tree and finds two div elements but only one has the p. Therefore, the div which contains p will be filled with yellow.
In this example you can see that the has() method filters out the div elements using the selector passed to this method, that is a p element. Below is the output produced by above HTML page.

Now we can look in to jQuery not(), another useful filtering method .

jQuery not()

The not() method filters out all the elements which matches the specified selector from the matched set of elements.

Here is the general syntax for using not():

element could be a string containing the selector expression, a function, any DOM element or any existing jQuery object.

jQuery not() example

Following example demonstrates the jQuery not() usage

In this example, we have three div elements and two of them is defined with CSS style class="green".
$( "div" ).not( ".green" ).css( "border-color", "red" ); This line of code filters out the div elements which does not have class="green" and changes the border color to red.

jquery-not-example

Now we can look in to jQuery filter(), another useful filtering method .

jQuery filter()

The filter() method removes all the elements from the matched set of elements that do not match the specified selectors.

Here is the general syntax for using filter():

element could be a string containing the selector expression, a function, any DOM element or any existing jQuery object.

jQuery filter() example

Following example demonstrates the jQuery filter() usage

In this example, we have three div elements and one of them is defined with CSS style class="newDivClass".
First we change the back ground color of all the div elements to yellow and changes the border color of the div element which has clas==”newDivClass” using filter() method.

I hope you have understood the use of these filtering methods in jQuery. We will discuss other methods in the coming posts.

By admin

Leave a Reply

%d bloggers like this: