Prototype - previous() Method



This method returns the element's previous sibling or the index'th one, if index is specified that matches cssRule. If no cssRule is provided, all previous siblings are considered. If no previous sibling matches these criteria, undefined is returned.

Syntax

element.previous([cssRule][, index = 0] );

Return Value

Returns HTML element. If no previous sibling matches these criteria, undefined is returned.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = $('saying').previous();
            alert( "$('saying').previous() " + str.innerHTML );

            var str = $('saying').previous(0);
            alert( "$('saying').previous(0) " + str.innerHTML  );
   
            var str = $('saying').previous('h3');
            alert( "$('$('saying').previous('h3') " + str.innerHTML  );

            var str = $('ida-red').previous( '.yummy');
            alert( "$('ida-red').previous( .yummy ) " + str.innerHTML );

            var str = $('ida-red').previous( 5 );
            alert( "$('ida-red').previous() " + str );
         }
      </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