jQuery Topics
- jQuery Introduction
- jQuery Callbacks
- jQuery deferred
- jQuery selectors
- *
- :animated
- [name|=”value”]
- [name*=”value”]
- [name~=”value”]
- [name$=”value”]
- [name=”value”]
- [name!=”value”]
- [name^=”value”]
- :button
- :checkbox
- :checked
- Child Selector
- .class
- :contains()
- Descendant Selector
- :disabled
- Element
- :empty
- :enabled
- :eq()
- :even
- :file
- :first-child
- :first-of-type
- :first
- :focus
- :gt()
- Has Attribute
- :has()
- :header
- :hidden
- #id
- :image
- :input
- :lang()
- :last-child
- :last-of-type
- :last
- :lt()
- [name=”value”][name2=”value2″]
- (“selector1, selector2, selectorN”)
- (“prev + next”)
- (“prev ~ siblings”)
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :odd
- :only-child
- :only-of-type
- :parent
- :password
- :radio
- :reset
- :root
- :selected
- :submit
- :target
- :text
- :visible
- jQuery Ajax Events
- jQuery Ajax Methods
- jQuery Keyboard Events
- jQuery Keyboard Methods
- jQuery Form Events
- jQuery Form Methods
- jQuery Mouse Events
- jQuery Mouse Methods
- jQuery Event Properties
- jQuery Event Methods
- jQuery HTML
- jQuery CSS
- jQuery Fading
- jQuery Traversing
- jQuery Utilities
- jQuery Properties
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:
$(":not(selector)")
📝 Example
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.htmlCopied<div class="item">Item 1</div> <div class="item special">Special Item</div> <div class="item">Item 2</div>
example.jsCopied$(".item:not(.special)").css("color", "blue");
This will change the color of all .item elements except those with the class .special to blue.
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.htmlCopied<div id="element1">Element 1</div> <div id="element2">Element 2</div>
example.jsCopied$("div:not(#element1)").css("font-weight", "bold");
This will make all <div> elements except the one with the ID element1 bold.
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.htmlCopied<input type="text" id="input1"> <input type="text" id="input2" disabled> <input type="text" id="input3">
example.jsCopied$("input:not([disabled])").css("border-color", "green");
This will give a green border to all <input> elements except those with the disabled attribute.
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.jsCopied$("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:
Author
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
If you have any doubts regarding this article (jQuery :not() Selector), please comment here. I will help you immediately.