Prototype - descendantOf() Method



This method checks if element is a descendant of ancestor.

As Element.descendantOf internally applies $() to ancestor, it accepts indifferently an element or an element's id as its second argument.

Syntax

element.descendantOf(ancestor);

Return Value

If it finds that element is a decendant of an ancestor, then it returns true, otherwise false.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function isDescendant() {
            var father = $('father');
            var kid = $('kid');
            
            // This is correct relationship and will be printed
            if( kid.descendantOf(father) ) {
               alert( "Kid is descendant of father" );
            }
            
            // This is wrong relationship and will not be printed
            if( father.descendantOf(kid) ) {
               alert( "Father is descendant of kid" );
            }
         }
      </script>
   </head>

   <body>
      <p>Click isDescendant button to see the result.</p>
      <div id = "grandfather">
         <div id = "father">
            <div id = "kid"></div>
         </div>
      </div>
      <br />
      
      <input type = "button" value = "isDescendant" onclick = "isDescendant();"/>
   </body>
</html>

Output

prototype_element_object.htm
Advertisements