Prototype - addMethods() Method


Advertisements


This method makes it possible to mix in your own methods to the Element object, which you can later use as methods of extended elements.

To add new methods, simply feed Element.addMethods with a hash of methods. Note that each method's first argument has to be an element.

Syntax

element.addMethods([hash of methods]);

OR

element.addMethods(tagName, methods);

Here, second form of the method will make added method available for a particular tag only.

Return Value

None.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         // Make changeColor method available for all the elements
         Element.addMethods({
            changeColor: function(element, colorName) {
               element = $(element);
               element.style.color = colorName;
               return element;
            }
         });
         function ShowEffect() {
            node = $("firstDiv");
         
            // Now call changeColor method
            node.changeColor( "red" );
         }
      </script>
   </head>
   
   <body>
      <div id = "firstDiv">
         <p>This is first paragraph</p> 
      </div>
      <br />
      
      <input type = "button" value = "ShowEffect" onclick = "ShowEffect();"/>
   </body>
</html>

Output


prototype_element_object.htm

Advertisements