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 Descendant Selector
Photo Credit to CodeToFun
🙋 Introduction
jQuery offers a variety of selectors to target specific HTML elements within a document, and one of the most versatile among them is the descendant selector. This selector allows you to select elements that are descendants of another element, providing powerful capabilities for DOM traversal and manipulation.
In this guide, we'll explore the jQuery descendant selector in depth, with clear examples to illustrate its usage and potential.
🧠 Understanding Descendant Selector
The descendant selector in jQuery enables you to select elements that are descendants of another element, no matter how deeply nested they are in the DOM hierarchy. This allows you to target specific elements within a broader context, making it invaluable for tasks such as styling, event handling, and content manipulation.
💡 Syntax
The syntax for the Descendant
selector is straightforward:
$("parentElement descendantElement")
📝 Example
Selecting Descendant Elements:
Suppose you have a <div> element with several <p> elements nested inside it, and you want to select all the paragraphs. You can use the descendant selector as follows:
index.htmlCopied<div id="container"> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> </div>
example.jsCopied$("#container p").css("color", "red");
This will set the text color of all paragraphs within the #container div to red.
Handling Events on Descendant Elements:
You can bind events to descendant elements using jQuery. Let's say you want to display an alert when any <button> inside a <div> with a specific class is clicked:
index.htmlCopied>div class="container"> >button>Button 1>/button> >button>Button 2>/button> >/div>
example.jsCopied$(".container button").click(function() { alert("Button clicked!"); });
Manipulating Descendant Elements' Content:
You can also manipulate the content of descendant elements dynamically. For example, let's change the text of all <span> elements within a <div>:
index.htmlCopied<div id="container"> <span>Text 1</span> <span>Text 2</span> </div>
example.jsCopied$("#container span").text("New Text");
This will set the text of all <span> elements within the #container div to "New Text".
Filtering Descendant Elements:
If you want to filter descendant elements based on certain criteria, jQuery offers various methods like filter() and find() to refine your selection.
🎉 Conclusion
The jQuery descendant selector is a powerful tool for selecting and manipulating elements within a specific context. Whether you're styling elements, handling events, or manipulating content, this selector provides a flexible and efficient solution.
By mastering its usage, you can streamline your DOM traversal and manipulation tasks, making your code more concise and maintainable.
👨💻 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 Descendant Selector), please comment here. I will help you immediately.