- Method 1: Using the Node.contains() method
The Node.contains() method is used to check if a given node is the descendant of another node at any level. The descendant may be directly the child’s parent or further up the chain. It returns a boolean value of the result.This method is used on the parent element and the parameter passed in the method is the child element to be checked. It returns true if the child is a descendant of the parent. This means that the element is a child of the parent.
Syntax:
functioncheckParent(parent, child) {if(parent.contains(child))returntrue;returnfalse;}chevron_rightfilter_noneExample:
<html><head><title>How to Check if an element is achild of a parent using JavaScript?</title><style>.parent,.child,.non-child {border: 2px solid;padding: 5px;margin: 5px;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>How to Check if an element isa child of a parent using JavaScript?</b><divclass="parent">This is the parent div.<divclass="child">This is the child div.</div></div><divclass="non-child">This is outside the parent div.</div><p>Click on the button to check if theelements are child of a parent.</p><p>Child has parent:<spanclass="output-child"></span></p><p>Non-Child has parent:<spanclass="output-non-child"></span></p><buttononclick="checkElements()">Check elements</button><script>function checkParent(parent, child) {if (parent.contains(child))return true;return false;}function checkElements() {parent = document.querySelector('.parent');child = document.querySelector('.child');non_child = document.querySelector('.non-child');output_child = checkParent(parent, child);output_non_child = checkParent(parent, non_child);document.querySelector('.output-child').textContent =output_child;document.querySelector('.output-non-child').textContent =output_non_child;}</script></body></html>chevron_rightfilter_noneOutput:
- Before clicking the button:
- After clicking the button:
- Before clicking the button:
- Method 2: Looping through the parents of the given child
The child can be checked to have the given parent by continuously looping through the element’s parents one by one. The parent of each node is found by accessing the parentNode property which returns the parent node if any.A while loop is used until the parent required is found or no more parent elements exist. Inside this loop, each element’s parent node is found in every iteration. If the parent node matches the given one in any iteration, it means that the element is a child of the parent.
Syntax:
functioncheckParent(parent, child) {let node = child.parentNode;// keep iterating unless nullwhile(node !=null) {if(node == parent) {returntrue;}node = node.parentNode;}returnfalse;}chevron_rightfilter_noneExample:
<html><head><title>How to Check if an element is achild of a parent using JavaScript?</title><style>.parent,.child,.non-child {border: 2px solid;padding: 5px;margin: 5px;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>How to Check if an element isa child of a parent using JavaScript?</b><divclass="parent">This is the parent div.<divclass="child">This is the child div.</div></div><divclass="non-child">This is outside the parent div.</div><p>Click on the button to check ifthe elements are child of a parent.</p><p>Child has parent:<spanclass="output-child"></span></p><p>Non-Child has parent:<spanclass="output-non-child"></span></p><buttononclick="checkElements()">Check elements</button><script>function checkParent(parent, child) {let node = child.parentNode;// keep iterating unless nullwhile (node != null) {if (node == parent) {return true;}node = node.parentNode;}return false;}function checkElements() {parent = document.querySelector('.parent');child = document.querySelector('.child');non_child = document.querySelector('.non-child');output_child = checkParent(parent, child);output_non_child = checkParent(parent, non_child);document.querySelector('.output-child').textContent =output_child;document.querySelector('.output-non-child').textContent =output_non_child;}</script></body></html>chevron_rightfilter_noneOutput:
- Before clicking the button:
- After clicking the button:
- Before clicking the button:
Recommended Posts:
- How to remove parent element except its child element using jQuery ?
- How to get the child element of a parent using JavaScript ?
- How to call a parent method from child class in JavaScript?
- jQuery | parent > child Selector
- How to apply style to parent if it has child with CSS?
- How to navigate to a parent route from a child route?
- How to count all child elements of a particular element using JavaScript ?
- How to get position of an element relative to the document or parent using jQuery ?
- How to count child element using jQuery ?
- How to select all children of an element except the last child using CSS?
- JQuery | Get the n-th level parent of an element
- Remove all the child elements of a DOM node in JavaScript
- How to get the child node index in JavaScript?
- How to make flexbox children 100% height of their parent using CSS?
- Run Python script from Node.js using child process spawn() method
- How to select all child elements recursively using CSS?
- How to get specific number of child elements using CSS?
- How to iterate through child elements of a div using jQuery ?
- Check whether HTML element has scrollbars using JavaScript
- How to Check an Element with Specific ID Exists using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


