Copyright © tutorialspoint.com

Prototype addMethods() Method

previous next


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

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>

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


previous next

Copyright © tutorialspoint.com