Page Content

Tutorials

What are the Parent/Child Selector in jQuery With Example

Parent/Child Selector

The child combinator (parent > child selection) is a key jQuery feature for accurately identifying elements in the Document Object Model (DOM). This CSS selector is one of many used by jQuery to make accessing items easy. In jQuery, the parent > child selector (child combinator) permits precise selection of Document Object Model elements. In this selector, A is the parent and B is its direct child. This is usually wrapped in the $( ) factory function in jQuery, such as $(‘div > p’).

This limits selection to items that are immediate children of a element, rather than all descendants. How this works requires understanding the DOM as a tree with obvious parent-child relationships. For example, $(‘#selected-plays > li’) targets only the top-level list items (direct children) of an unordered list with the ID selected-plays, allowing style alterations like adding a horizontal class to those immediate children.

Direct targeting is important since larger descendant selectors (e.g., A B) choose any components B nested anywhere within A regardless of direct parentage. Developers can easily access and edit elements using the parent > child selection, easing HTML document traversal and improving web development.

Syntax:

A > B is the child combinator’s general syntax.

A > B
  • A is the parent element.
  • denotes a direct child relationship since it is the child combinator.
  • B stands for the child element that you wish to choose.

This syntax, such as $(‘A > B’), is contained within the $( ) factory function when it is used in jQuery. The jQuery() function is contained in the $( ) function itself.

How it Works

A web page’s DOM organises its elements into parents, children, and descendants like a family tree.

  • An element nested inside another is considered its child, and the outer element is the parent.
  • Particularly, items that are direct offspring of a given parent are targeted by the parent > child selector. This is significant since it indicates that only items that are directly beneath the parent and one level deep will be chosen.
  • A space in place of >, for example, would choose all descendants of A, regardless of how many layers deep they are (A B). Only the immediate children will be sent to you, were the >.

This expression is processed by the selection engine of jQuery, which looks for elements in the DOM tree that exactly match this direct parent-child connection.

Code Examples:

The following examples illustrate the child selector:

<!DOCTYPE html>
<html>
<head>
  <title>Child Selector Example</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(document).ready(function(){
      // Select <p> elements that are direct children of <div>
      $("div > p").css("color", "blue");
    });
  </script>
</head>
<body>
<div>
  <p>This paragraph is a direct child of div.</p>
  <div>
    <p>This paragraph is nested inside another div.</p>
  </div>
</div>
<p>This paragraph is outside the div.</p>
</body>
</html>

Output:

This paragraph is a direct child of div.
This paragraph is nested inside another div.

This paragraph is outside the div.
  • If elements are direct descendants of elements, edit their text.
  • This code only impacts tags within a , not tags.
  • Sort all elements that are direct descendants of an unordered list with the ID selected-plays into a horizontal class using the top-level list classification.
  • This guarantees that no nested list elements, only the top-level list items (direct children of #selected-plays), receive the class.
  • General Selection of Direct Children You have the option to choose every element that is a direct child of a particular tag, such as a paragraph element.
  • Choosing a Certain Type of Child Using a nested navigation menu, you can choose every element that is a direct child of a element.

Conclusion

The parent > child selection (also known as the child combinator) in jQuery is a powerful and accurate tool for selecting only the direct offspring of a parent element in the HTML DOM. This selector restricts actions to elements one level below the parent to avoid inadvertent selections.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index