Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery Selectors

jQuery :not() Selector

Posted in jQuery Tutorial
Updated on Apr 26, 2024
By Mari Selvan
👁️ 6 - Views
⏳ 4 mins
💬 0
jQuery :not() Selector

Photo Credit to CodeToFun

🙋 Introduction

In jQuery, the :not() selector is a handy tool that allows you to select elements that do not match a specific criteria. This powerful selector opens up a multitude of possibilities for targeting elements based on their attributes, classes, or other characteristics. Understanding how to use the :not() selector effectively can greatly enhance your ability to manipulate DOM elements and create dynamic web pages.

In this guide, we'll explore the usage of the jQuery :not() selector with clear examples to help you grasp its potential.

🧠 Understanding :not() Selector

The :not() selector is designed to target elements that do not match a specified selector. It allows you to exclude certain elements from your selection, making it useful for fine-tuning your jQuery queries.

💡 Syntax

The syntax for the :not() selector is straightforward:

syntax.js
Copied
Copy To Clipboard
$(":not(selector)")

📝 Example

  1. Selecting Elements Except a Specific Class:

    Suppose you have a list of elements with different classes, and you want to select all elements except those with a specific class. You can achieve this using the :not() selector as follows:

    index.html
    Copied
    Copy To Clipboard
    <div class="item">Item 1</div>
    <div class="item special">Special Item</div>
    <div class="item">Item 2</div>
    example.js
    Copied
    Copy To Clipboard
    $(".item:not(.special)").css("color", "blue");

    This will change the color of all .item elements except those with the class .special to blue.

  2. Selecting Elements Except a Specific ID:

    If you want to select all elements except the one with a specific ID, you can use the :not() selector accordingly:

    index.html
    Copied
    Copy To Clipboard
    <div id="element1">Element 1</div>
    <div id="element2">Element 2</div>
    example.js
    Copied
    Copy To Clipboard
    $("div:not(#element1)").css("font-weight", "bold");

    This will make all <div> elements except the one with the ID element1 bold.

  3. Selecting Elements Except Those with a Specific Attribute:

    You can also select elements based on their attributes using the :not() selector. For example, to select all <input> elements except those with the disabled attribute:

    index.html
    Copied
    Copy To Clipboard
    <input type="text" id="input1">
    <input type="text" id="input2" disabled>
    <input type="text" id="input3">
    example.js
    Copied
    Copy To Clipboard
    $("input:not([disabled])").css("border-color", "green");

    This will give a green border to all <input> elements except those with the disabled attribute.

  4. Combining :not() with Other Selectors:

    The :not() selector can be combined with other selectors for more precise targeting. For example, to select all <div> elements except those with a specific class and containing a certain text:

    example.js
    Copied
    Copy To Clipboard
    $("div:not(.special):contains('Hello')").css("background-color", "yellow");

🎉 Conclusion

The jQuery :not() selector is a powerful tool for selecting elements based on exclusion criteria. Whether you need to select elements based on classes, IDs, attributes, or other characteristics, this selector provides a flexible and efficient solution.

By mastering its usage, you can fine-tune your jQuery queries and create more dynamic and interactive web pages.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy