Prototype - ancestors() Method


Advertisements

This method collects all of element's ancestors and returns them as an array of extended elements.

Keep in mind that the body and html elements will also be included.

Syntax

element.ancestors();

Return Value

An array of HTML elements.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showElements() {
            var arr =  $('kid').ancestors();
            arr.each(function(node) {
               alert(node.nodeName + ': ' + node.innerHTML);
            });
         }
      </script>
   </head>

   <body>
      <div id = "father">
         <p id = "kid">This is first paragraph</p>
      </div>
      <br />
      
      <input type = "button" value = "showElements" onclick = "showElements();"/>
   </body>
</html>

Output

prototype_element_object.htm
Advertisements