This tutorial aims to teach you how to navigate parent, child, and sibling elements in jQuery. Understanding how to traverse the Document Object Model (DOM) tree is crucial to manipulate HTML elements effectively.
By the end of this tutorial, you will understand how to:
- Identify and select parent, child, and sibling elements using jQuery.
- Use these selections to manipulate HTML elements on your web pages.
Basic knowledge of HTML, CSS, JavaScript, and jQuery is required. If you're not familiar with these topics, consider reviewing related resources first.
Parent, child, and sibling elements are defined by their relationships in the DOM tree:
jQuery provides methods to navigate these relationships:
parent()
: This method gets the parent of the selected element(s).children()
: This method gets the children of the selected element(s).siblings()
: This method gets the siblings of the selected element(s).Here is an example of how to use these methods:
<div class="parent">
<div class="child">Child 1</div>
<div class="child">Child 2</div>
</div>
In jQuery:
$(".child").parent(); // Selects the parent of the child class elements
$(".parent").children(); // Selects the children of the parent class element
$(".child").siblings(); // Selects the siblings of the child class elements
Remember that these methods return jQuery objects, which means you can chain other jQuery methods onto them:
$(".child").parent().hide(); // Selects the parent and hides it
$(".parent").children().addClass('highlight'); // Selects the children and adds the 'highlight' class
$(".child").siblings().fadeOut(); // Selects the siblings and fades them out
Here are practical examples demonstrating how to find parent, child, and sibling elements:
// HTML
<div id="parent">
<p class="child">Hello, world!</p>
</div>
// jQuery
$(".child").parent().css("background-color", "yellow");
The parent of the .child
element will get a yellow background color.
// HTML
<ul id="parent">
<li class="child">One</li>
<li class="child">Two</li>
</ul>
// jQuery
$("#parent").children().css("color", "red");
All child elements of #parent
will have their text colored red.
// HTML
<div id="parent">
<p class="child">Hello</p>
<p class="child">World</p>
</div>
// jQuery
$(".child:first").siblings().css("text-decoration", "underline");
The sibling of the first .child
element will be underlined.
You've learned how to navigate parent, child, and sibling elements in jQuery. You now understand how to identify and select these elements and manipulate them on your web pages. The next step is to use these methods to create complex interactions and effects.
Given the following HTML structure:
<div class="box">
<p class="text">Hello</p>
<p class="text">World</p>
</div>
Write jQuery code to add a border to the parent of the .text
elements.
Using the same HTML structure, write jQuery code to make the text color of the .text
elements' siblings blue.
Again, using the same HTML structure, write jQuery code to hide the siblings of the first .text
element.
$(".text").parent().css('border', '1px solid black');
$(".text").siblings().css('color', 'blue');
$(".text:first").siblings().hide();
Practice these exercises until you're comfortable with the concepts. Be sure to experiment with different jQuery methods and selectors!