jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
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.