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 :has() Selector
Photo Credit to CodeToFun
🙋 Introduction
In the realm of jQuery, selectors play a crucial role in targeting specific elements within an HTML document. One such selector is the :has()
selector, which enables you to select elements based on whether they contain certain descendants. Understanding how to effectively use the :has()
selector can significantly enhance your ability to traverse and manipulate the DOM.
This comprehensive guide will walk you through the usage of the jQuery :has()
selector with clear examples to illuminate its functionality.
🧠 Understanding :has() Selector
The :has()
selector allows you to target elements that contain specific descendants, filtering the selection based on the existence of these descendants. This provides a powerful mechanism for selecting elements based on their content structure.
💡 Syntax
The syntax for the :has()
selector is straightforward:
$("selector:has(selector)")
📝 Example
Selecting Elements Containing Specific Descendants:
Suppose you have a list of <div> elements, and you want to select only those that contain <span> elements within them. You can achieve this using the
:has()
selector as follows:index.htmlCopied<div> <p>This div does not contain any span.</p> </div> <div> <span>This div contains a span.</span> </div> <div> <span>Another span-containing div.</span> <p>And it also contains a paragraph.</p> </div>
example.jsCopied$("div:has(span)").css("border", "2px solid red");
This will apply a red border to the <div> elements that contain <span> elements.
Styling Parent Elements Based on Child Elements:
You can use the
:has()
selector to style parent elements based on the presence of certain child elements. For example, let's add a background color to <div> elements containing <a> tags:index.htmlCopied<div> <p>This div does not contain any anchor.</p> </div> <div> <a href="#">This div contains a link.</a> </div> <div> <a href="#">Another link-containing div.</a> <p>And it also contains a paragraph.</p> </div>
example.jsCopied$("div:has(a)").css("background-color", "lightblue");
This will set the background color of <div> elements containing <a> tags to light blue.
Filtering Elements Based on Complex Conditions:
You can create more complex selectors by combining the
:has()
selector with other jQuery selectors and methods. For instance, let's select <li> elements that contain <a> tags and have a specific class:index.htmlCopied<ul> <li>This list item does not contain any link.</li> <li class="special"><a href="#">This list item contains a special link.</a></li> <li><a href="#">Another link-containing list item.</a></li> </ul>
example.jsCopied("li:has(a).special").css("font-weight", "bold");
This will make the text of the <li> element with both a <a> tag and the class special bold.
🎉 Conclusion
The jQuery :has()
selector offers a powerful means of selecting elements based on the presence of specific descendants. Whether you need to style parent elements, filter based on complex conditions, or perform other manipulations, this selector provides a flexible and efficient solution.
By mastering its usage, you can effectively traverse and manipulate the DOM to 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 :has() Selector), please comment here. I will help you immediately.