Web | This Site

Learn Prototype
Prototype Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# 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>

To understand it in better way you can Try it yourself.

Advertisement

Online Image Processing

Indian Baby Names