Selecting Elements by What They Don’t Match - J Query

Problem
You need to select a number of elements that don’t match a specific selector.

Solution

For this, jQuery gives us the :not filter, which you can use in the following way:

This filter will remove any elements from the current collection that are matched by the passed selector. The selector can be as complex as you like; it doesn’t have to be a simple expression, e.g.:

Discussion
In addition to the mentioned :not filter, jQuery also supplies a method with very similar functionality. This method accepts both selectors and DOM collections/nodes. Here’s an example:

According to this code, when an anchor is clicked, all anchors apart from that one will have the class not-clicked added. The this keyword refers to the clicked element.

The not() method also accepts selectors:

This code selects all anchors residing within #nav that do not have a class of active.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics