Dojo is a JavaScript toolkit that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and much more.
isDescendant()
in dojo/dom?The isDescendant
method can be used to check if a DOM node is a descendant (child) of an ancestor (parent) node. In simple words, checks if a node is a child node of the given parent node. The isDescendant
method is present in the dom
module of the Dojo library.
isDescendant(node, ancestorNode);
This method takes two arguments:
node
: The node to be checked if it is a child element.
ancestorNode
: The node inside which the child node availability is checked.
The argument type can be either string or a DOM node. If we pass a string then the string value is considered as the id
of the element.
This method returns a boolean value. It will return true
if the provided node
argument (first argument) is a child element of the ancestorNode
(second) argument, and false
otherwise.
Let's look at the code below:
In the above code snippet:
Lines 6–8: We have created a div
element with an id
value parent
. We have added another div
element inside the parent element with an id
value child-1
.
Line 9: We have created a div
element with an id
value unknown-child
.
Line 11: We load the Dojo library from the CDN server.
Line 13: We load the dojo/dom
module that contains the isDescendant
method.
Line 14: We use the isDescendant
method to check if the element with an id
value child-1
is a child element of the element with an id
value parent
. For this method, we pass id strings as arguments. In our case, we have a child-1
element present inside the parent
element so true
is returned.
Lines 18–19: We use the byId
method to get the node element by providing the id string of the element. Using this method we get the element with id
values parent
and child-1
and assign it to the parent
and child1
variables respectively.
Line 21: We use the isDescendant
method to check if the element child1
is a descendant(child) of the parent
. For this method, we pass the DOM node as an argument. In our case, we have a child-1
element present inside the parent
element so true
is returned.
Line 24: We use the isDescendant
method to check if the element with id
value unknown-child
is a child element of the element with id
value parent
. For this method, we pass id strings as arguments. In our case, the unknown-child
element is not the child element of the parent
element so false
is returned.
Free Resources