jquery
  1. jquery-not

JQuery not()

The not() function in jQuery is used to exclude the elements specified by a selector from a selected set of elements.

Syntax

The basic syntax for using the not() function in jQuery is as follows:

$(selector).not(excluded-selector);

In this syntax, selector refers to the initial set of elements to be included, while excluded-selector is the selector that specifies which elements to exclude from the selection.

Use

The not() function is useful when you want to apply a specific action to a group of elements, but you want to exclude certain elements from that group. For example, you might want to apply a background color to all paragraph elements on a page, except for those within a specific section.

Example

Here is an example of using the not() function to exclude certain elements from a selection:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Exclude Elements Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script>
    $(document).ready(function(){
      $("p:not(.exclude)").addClass("highlight");
    });
  </script>
  <style>
    .highlight {
      background-color: yellow;
    }
  </style>
</head>
<body>
  <h2>Exclude Elements Example</h2>
  <p>This is the first paragraph.</p>
  <p class="exclude">This paragraph is excluded.</p>
  <p>This is the third paragraph.</p>
</body>
</html>
Try Playground

In this example, all paragraph elements on the page will have a yellow background color applied to them, except for those with the class "exclude". These excluded elements will be unaffected by the CSS action.

Summary

The not() function in jQuery is a useful tool for excluding certain elements from a selection set, allowing for a more fine-tuned and specific set of actions to be applied to the remaining elements. It can be useful in a variety of situations where customization and specificity are important.

Published on: