jQuery Hierarchy Selectors Tutorial 🎯

beginner
10 min

jQuery Hierarchy Selectors Tutorial 🎯

Welcome to our deep dive into jQuery Hierarchy Selectors! In this lesson, we'll explore how to select elements based on their hierarchical relationships in a web document. 📝 Remember, understanding hierarchy selectors will empower you to navigate through complex HTML structures with ease.

What are Hierarchy Selectors? 📝

Hierarchy Selectors in jQuery allow you to select elements based on their position within the HTML structure. They help you to target elements that are parents, children, siblings, or ancestors of a specified element.

Parent Selector ($("parent selector")) 💡

The parent selector is used to select the parent element of a specified child element.

Example:

html
<div id="parent"> <p>Child Element</p> </div>

To select the parent div, we can use the following jQuery code:

javascript
$('#parent');

Tip: To select the immediate parent, use the > symbol. For instance, $('div > p') will select p elements that are immediate children of a div.

Child Selector ($("parent selector > child selector")) 💡

The child selector selects all the direct children of a specified parent element.

Example:

html
<div id="parent"> <p>Child 1</p> <ul> <li>Grandchild 1.1</li> <li>Grandchild 1.2</li> </ul> <p>Child 2</p> </div>

To select all the p children of the div, we can use the following jQuery code:

javascript
$('div > p');

Sibling Selectors ($("selector + selector") or $("selector ~ selector")) 💡

Sibling Selectors allow you to select elements that share the same parent and have the specified order. There are two types of sibling selectors:

  1. Adjacent Sibling Selector ($("selector + selector")): Selects the direct sibling that immediately follows the specified sibling.

Example:

html
<div id="parent"> <p>First Sibling</p> <ul> <li>Child Element</li> </ul> <p>Second Sibling</p> </div>

To select the ul element that follows the first p element, we can use the following jQuery code:

javascript
$('p + ul');
  1. General Sibling Selector ($("selector ~ selector")): Selects all the siblings that follow the specified sibling, regardless of their order.

Example:

html
<div id="parent"> <p>First Sibling</p> <ul> <li>Child Element 1</li> </ul> <p>Second Sibling</p> <ul> <li>Child Element 2</li> </ul> </div>

To select both ul elements, we can use the following jQuery code:

javascript
$('p ~ ul');

Ancestor Selectors ($("ancestor selector descendant selector")) 💡

Ancestor Selectors select elements that are descendants of a specified ancestor.

Example:

html
<div id="grandparent"> <div id="parent"> <p>Child Element</p> </div> <ul id="ancestor-example"> <li>Ancestor Example</li> </ul> </div>

To select the ul element that is a descendant of the div with the id grandparent, we can use the following jQuery code:

javascript
$('div#grandparent descendant ul');

Putting it all together 💡

Now that we've learned about the different types of hierarchy selectors, let's practice with an example:

html
<div id="parent-container"> <div id="parent1"> <p>Child 1</p> <ul> <li>Grandchild 1.1</li> <li>Grandchild 1.2</li> </ul> </div> <div id="parent2"> <p>Child 2</p> </div> </div>

Try to write the jQuery code to select:

  1. The div with the id parent1
  2. All the p elements that are direct children of div with the id parent2
  3. The ul that is a sibling of the div with the id parent1
  4. All the li elements that are descendants of the div with the id parent-container

Answers:

  1. $('#parent1')
  2. $('#parent2') > 'p'
  3. $('#parent1') + ' ul'
  4. $('#parent-container') > '* > ul > li'
Quick Quiz
Question 1 of 1

Which jQuery code selects the `div` with the id `parent1` from the provided example?

Quick Quiz
Question 1 of 1

Which jQuery code selects all the `p` elements that are direct children of the `div` with the id `parent2` from the provided example?

Quick Quiz
Question 1 of 1

Which jQuery code selects the `ul` that is a sibling of the `div` with the id `parent1` from the provided example?

Quick Quiz
Question 1 of 1

Which jQuery code selects all the `li` elements that are descendants of the `div` with the id `parent-container` from the provided example?