Search⌘ K

Solution Review: Is a Child?

Explore how to implement and understand a JavaScript function that checks whether a given element is a child or descendant of another. This lesson helps you practice DOM traversal methods crucial for front-end coding interviews and solidifies your knowledge of parent-child relationships in the DOM tree.

We'll cover the following...

Solution #

Explanation #

Let’s discuss the JavaScript code for isChild.

Javascript (babel-node)
function isChild(parent, child){
while(child.parentNode){
if(child.parentNode == parent)
return true;
else
child = child.parentNode
}
return false
}

It takes two parameters, the parent and the child element. First, the function checks if the child has a ...