Prototype - previousSiblings() Method



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

Two elements are siblings if they have the same parent. So for example, the head and body elements are siblings (their parent is the html element). Previous siblings are simply the ones which precede element in the document.

Syntax

element.previousSiblings();

Return Value

Returns an array of HTML elements.

Example

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

   <body>
      <p>Click the button to see the result.</p>
      
      <ul id = "fruits">
         <li id = "apples">
            <h3 id = "title">Apples</h3>
            <ul id = "list-of-apples">
               <li id = "golden-delicious">Golden Delicious</li>
               <li id = "mutsu">Mutsu</li>
               <li id = "mcintosh" class = "yummy">McIntosh</li>
               <li id = "ida-red" class = "yummy">Ida Red</li>
            </ul>
            <p id = "saying">An apple a day keeps the doctor away.</p>  
         </li>
      </ul>
      <br />
      
      <input type = "button" value = "Show Result" onclick = "showResult();"/>
   </body>
</html>

Output

prototype_element_object.htm
Advertisements