What is isDescendant() in dojo/dom?
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.
What is 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.
Syntax
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.
Return value
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.
Example
Let's look at the code below:
Explanation
In the above code snippet:
Lines 6–8: We have created a
divelement with anidvalueparent. We have added anotherdivelement inside the parent element with anidvaluechild-1.Line 9: We have created a
divelement with anidvalueunknown-child.Line 11: We load the Dojo library from the CDN server.
Line 13: We load the
dojo/dommodule that contains theisDescendantmethod.Line 14: We use the
isDescendantmethod to check if the element with anidvaluechild-1is a child element of the element with anidvalueparent. For this method, we pass id strings as arguments. In our case, we have achild-1element present inside theparentelement sotrueis returned.Lines 18–19: We use the
byIdmethod to get the node element by providing the id string of the element. Using this method we get the element withidvaluesparentandchild-1and assign it to theparentandchild1variables respectively.Line 21: We use the
isDescendantmethod to check if the elementchild1is a descendant(child) of theparent. For this method, we pass the DOM node as an argument. In our case, we have achild-1element present inside theparentelement sotrueis returned.Line 24: We use the
isDescendantmethod to check if the element withidvalueunknown-childis a child element of the element withidvalueparent. For this method, we pass id strings as arguments. In our case, theunknown-childelement is not the child element of theparentelement sofalseis returned.
Free Resources