Page Content

Tutorials

How to Use :Not selector in jQuery With Code Example

:Not selector in jQuery

The powerful jQuery :not(selector) filtering selector removes items from a matched set that meet a requirement. This unique jQuery extension to CSS selectors gives website users extra alternatives when selecting objects.

Syntax:

The :not(selector’s general syntax is as follows:

selector.not( selector )

A single CSS selector or a set of selectors separated by commas to apply multiple filters at once, such as not(“.class1,.class2”), can be used as the selector argument. A callback function can also be passed to the.not() method for more intricate filtering situations.

How it Works

Its main role is to choose every element in a result set that does not match the selector in parenthesis. When applied, jQuery eliminates components that fit the :not() condition from the collection. Using $(‘li:not(.myclass)’) selects all list items () except “myclass.” This targets only elements without the class.

By taking in a callback function, the.not() technique provides superior filtering capabilities over basic CSS selectors. The code is executed for each matching element as jQuery iterates through them when a callback function is supplied. If an element’s callback function returns true, it is then removed from the final result set. Filtering according to criteria that are not expressible solely through CSS selectors is made possible by this programming method.

Code Example

Let’s have a look at a real-world example that is used to demonstrate how :not selector functions:

<!DOCTYPE html>
<html>
<head>
  <title>jQuery :not Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <style>
    p { padding: 10px; margin: 5px; border: 1px solid #ccc; }
    .intro { background: #e0f7fa; color: #00796b; }
    .important { background: #fff3e0; color: #e65100; font-weight: bold; }
  </style>
</head>
<body>
<p class="intro">Intro paragraph (visible)</p>
<p>Regular paragraph (hidden)</p>
<p class="important">Important paragraph (visible)</p>
<p>Another regular paragraph (hidden)</p>
<button id="filterBtn">Hide Regular Paragraphs</button>
<script>
  $("#filterBtn").click(function() {
    $("p:not(.intro):not(.important)").hide();
  });
</script>
</body>
</html>

Output:

Intro paragraph (visible)

Regular paragraph (hidden)
Important paragraph (visible)

Another regular paragraph (hidden)
Hide Regular Paragraphs

Explanation of the Code

$(document).ready(function() { … });: This common jQuery approach delays JavaScript code until HTML content loads and DOM manipulation is possible. This avoids jQuery issues when selecting or changing rendered items.

$(“#filterBtn”).click(function() { … });: This line is directed at the HTML button> element with filterBtn set for the id property. This button then has a click event handler attached to it. The code specified in the click() method will be run each time the user clicks this button.

$(“p:not(.intro, .important)”): Use :not() instead.

  • Initially, the “p” component addresses all <p> (paragraph) elements in HTML content.
  • The initial paragraphs are filtered using :not(.intro,.important). It tells jQuery that any <p> element that contains the class important or the class intro should be removed. Essentially, only “regular” paragraphs without these particular stylistic classes are chosen.

.hide(“slow”): The.hide() function is applied to the paragraphs that meet the :not() criterion after they have been chosen. The hiding effect should happen gradually over time, not instantly, according to the “slow” setting. The items are essentially removed from the page flow and succeeding elements shift upwards as a result of jQuery’s hidden() function, which also sets the elements’ show attribute to none.

All things considered, the :not selector is a flexible tool for honing element selections in jQuery by defining exclusion criteria, giving you exact control over which items are modified.

Conclusion

A strong filtering mechanism in jQuery, the :not selector improves your ability to precisely manage which components are impacted by your scripts. It streamlines intricate reasoning and enhances code readability by enabling you to eliminate elements from a selection according to one or more criteria. Beyond what normal CSS selectors can accomplish, :not() provides a great deal of flexibility, whether you’re utilising basic class exclusions or custom callback methods for more complex filtering. With clearer, easier-to-maintain jQuery code, developers may improve user experience and streamline interactions in real-world scenarios like conditionally applying styles or concealing non-highlighted material.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index