The Wayback Machine - https://web.archive.org/web/20240927093722/https://www.geeksforgeeks.org/jquery-parent-child-selector/
Open In App

jQuery parent > child Selector

Last Updated : 14 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

It is a jQuery Selector used to select all elements that are the direct child of its parent element. 

Syntax:

("parent > child")

Parameter Values:

  • parent: Using this, the parent element will be selected.
  • child: Using this, the direct child element of the specified parent element will be selected.

Example 1: 

HTML




<!DOCTYPE html>
<html>
<h1>
  <center>Geeks </center>
</h1>
<head>
  <script src=
  </script>
  <script>
    $(document).ready(function () {
      $("div > span").css(
        "background-color", "green");
    });
  </script>
</head>
<body>
  <h3>All <span> elements that are
    a direct child of <div>
      element are selected.</h3>
  <div style="border:1px solid black;">
    <p>Geeks for Geeks
      <br>
      <span>
        jQuery parent > child Selector
      </span>
    </p>
  </div>
  <br>
  <div style="border:1px solid black;">
    <p>Geeks for Geeks</p>
    <span>
      jQuery parent > child Selector
    </span>
  </div>
</body>
</html>


Output:

  

Example 2: 

HTML




<!DOCTYPE html>
<html>
<h1>
  <center>Geeks </center>
</h1>
<head>
  <script src=
  </script>
  <script>
    $(document).ready(function () {
      $("div").css(
        "background-color", "green");
    });
  </script>
</head>
<body>
  <h3>All <div> elements
    are selected.</h3>
  <div style="border:1px solid black;">
    <p>Geeks for Geeks</p>
    <p>jQuery parent > child Selector
    </p>
  </div>
</body>
</html>


Output:

 



Previous Article
Next Article

Similar Reads

How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to remove parent element except its child element using jQuery ?
Given an HTML document and the task is to remove the parent element except for its child element. Approach 1: Use contents() method to select all the direct children, including text and comment nodes for the selected element.Selected elements are stored in a variable.Now, use replaceWith() method to replace the content of parent element by its all
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
jQuery :parent Selector
The jQuery :parent selector is used to select all elements that are the parent of another element, including text nodes. Syntax: $(":parent")Example 1: In this example, we will select all the parent elements of the <li> by using jQuery :parent Selector. C/C++ Code <!DOCTYPE html> <html> <head> <title> jQuery :parent()
1 min read
jQuery parent descendant Selector
jQuery parent descendant Selector selects every element which is a descendant to a specific(parent) element. Syntax: $("parent descendant")Example 1: In this example, we are using parent descendant selector. C/C++ Code <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min
1 min read
How to get/set elements containing the closest parent element matches the specified selector using jQuery ?
In this article, we will learn how to get a set of elements containing the closest parent element that matches the specified selector, the starting element included using jQuery. JQuery is the fastest and lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Mode
2 min read
jQuery :nth-last-child() Selector
The jQuery :nth-last-child() selector is used to select all elements that are the nth last child of their parent. The counting of elements starts from the last element. Syntax: :nth-last-child( n | even | odd | formula )Parameters: The :nth-last-child() selector contain parameters which are listed below: n: It holds the child index number of the se
2 min read
jQuery :last-child Selector
It is a jQuery Selector used to select every element that is the last child of its parent. Syntax: $(":last-child")Example 1: Here is the basic example of a last-child selector. C/C++ Code <!DOCTYPE html> <html> <head> <title>jQuery :last-child Selector</title> <script src= "https://ajax.googleapis.com/ajax/lib
1 min read
jQuery :only-child Selector
It is a jQuery Selector used to select every element that is the only child of it's parent i.e only a single child is selected. Syntax: $(":only-child")Example 1: In this example, we are using only-child selector. C/C++ Code <!DOCTYPE html> <html> <head> <title>jQuery :only-child Selector</title> <script src= "
1 min read
jQuery :nth-child() Selector
The jQuery :nth-child() Selector Selects all elements that are the nth-child of their parent elements. Syntax: $("Selector:nth-child( index | even |odd |equation )")Index: Index provided. Index starts fromeven: even number of child elements get selected.odd: odd number of child elements will get selected.equation: select child elements by solving a
1 min read
jQuery :first-child Selector
The :first-child selector allows you to select the first child of its parent element which makes manipulating DOM elements efficient. In this article, we will explore the jQuery :first-child selector, how to use it, and provide practical examples. What is the jQuery :first-child Selector?The :first-child selector in jQuery is used to target and man
2 min read
How to communicate from parent component to the child component in Angular 9 ?
Angular makes the communication between components very easy. In this article, we will learn how to communicate from a parent component to the child component. Approach: Let's create two components: parent child In the parent component, declare the property that you want to receive in the child component, say 'ParentId'. While including the child c
2 min read
How to navigate to a parent route from a child route?
In angular, a root component that serves as the parent component of all the components and the rest of the other components can be called a Child Component to the root component. This structure is in a form of a tree where a component that is the parent of a child lies above the child component and this there is no direct link between them but in g
4 min read
How to return true if the parent element contains the child element in JavaScript ?
In JavaScript, children property returns an array of those elements which are children of a given element. In order to check whether the parent element (say x) contains a child element (say y), we can use the children property to get the array of all children of x and then check whether the array obtained contains y. To get the child element of a p
2 min read
How to Style Parent Element when Hover Over a Child Element in CSS ?
In this article, we will learn how to style the parent element when hovering over a child element. Styling the parent element when hovering over a child element is a useful CSS technique that allows you to change the appearance of the parent element when the mouse cursor is hovering over one of its child elements. This can be useful for adding inte
2 min read
How to expand floated child div's height to its parent's height in CSS ?
Making the height of the floating child div equal to the height of the parent div is referred to as expanding the height of the floated child div to the height of the parent div. Approach 1: Using the CSS "clearfix" method is the first approach to increase a floated child div's height to match its parent's height. When an element is floated, it is
4 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
How to Check if an element is a child of a parent using JavaScript?
In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Using the Node.contains() methodThe Node.contains() me
4 min read
How to Access Child Method From the Parent in VueJS ?
In this article, we are going to discuss how can we access the child method from the parent in VueJS. We need to create such a component that shows the use of accessing the child methods from the parent component. Approach:We will create two components ParentMethod and ChildComponent.ParentMethod vue.js component consists of a parent component whic
3 min read
How does useImperativeHandle help parent and child components communicate?
useImperativeHandle in React allows parent and child components to communicate more effectively by enabling the child component to specify which methods and properties should be accessible to the parent through a ref. How Does It Help:Customized Interaction: Child components can define a specific set of functions or data they want to share with the
2 min read
How to Call Parent Components's Function from Child Component in React ?
In React, it is very important to manage communication between components for building flexible and maintainable applications. Sometime, you need to trigger a function defined in a parent component from a child component. In this article, we'll see how to achieve this in React by passing down functions as props from parent to child components. Tabl
3 min read
Passing data from Child to Parent Component in Angular
In Angular, passing data from a child component to its parent component involves emitting events. Essentially, the child component emits an event containing the data that the parent component needs to receive. This is typically achieved using Angular's EventEmitter class, where the child component emits an event with the data payload, and the paren
3 min read
How to Call a Parent Method from a Child Component in Vue 3 ?
Vue.js is a JavaScript framework for building user interfaces. In Vue 3 parent and child component can communicate with each other through props and events. Props are used to pass the data from the parent to the child component and events are used to pass the data from child to parent component. These are the following approaches: Table of Content
3 min read
Parent to Child Communication with Metadata in Angular 17
A parent component in Angular can transfer data or information to its child components using parent-to-child communication. This is an essential component of creating reusable and modular Angular applications. This is an overview of Angular 17's parent-to-child communication features. PrerequisitesBasic JavaScriptBasic AngularWhat is Parent to Chil
2 min read
How to call a parent method from child class in JavaScript?
Inheritance in programming allows a class to derive methods and properties from another class, similar to how a child inherits traits from its parents. A child class can have its properties in addition to inherited ones. The deriving class is called a derived, sub, or child class, while the class it derives from is the base, super, or parent class.
3 min read
Why margin on child element moves the parent element in CSS ?
In CSS, when a margin is applied to a child element, it can affect the position of the parent element. This happens because the margins of an element collapse with the margins of its adjacent siblings or its parent, based on certain rules. If the parent element has no padding or border, and the child element has a top margin, then the child element
3 min read
How to Call Function Inside Child Component from Parent Component using useRef hook ?
In ReactJS, we can pass the function reference from parent components to child components. However, there are scenarios where you may need to call the function inside the child component from the parent component. It can be done by creating a reference using the useRef hook and createRef method as well. Prerequisites :NPM & Node JSReact JSReact
2 min read
How to access index of the parent ng-repeat from child ng-repeat in AngularJS ?
In this article, we will see How to access the index of the parent ng-repeat from child ng-repeat in AngularJS. In AngularJS applications, when we implement the nested ng-repeat directives and if we need to access the index of the parent ng-repeat from the child ng-repeat, then we can do this by using 2 different approaches like by using the $paren
6 min read
How to get Access to a Child Method From the Parent in VueJS ?
When working with Vue.js components, it's common to encounter scenarios where you need to access a method inside the parent component that is defined in a child component. This can be useful for triggering specific actions or updating the child component's state. Below are the methods that can be used to access a child component method from a paren
4 min read