- Prototype Tutorial
- Prototype - Home
- Prototype - Short Overview
- Prototype - Useful Features
- Prototype - Utility Methods
- Prototype - Element Object
- Prototype - Number Processing
- Prototype - Strings Processing
- Prototype - Array Processing
- Prototype - Hash processing
- Prototype - Basic Object
- Prototype - Templating
- Prototype - Enumerating
- Prototype - Event Handling
- Prototype - Form Management
- Prototype - JSON Support
- Prototype - AJAX Support
- Prototype - Expressing Ranges
- Prototype - Periodical Execution
- Prototype Useful Resources
- Prototype - Quick Guide
- Prototype - Useful Resources
- Prototype - Discussion
Prototype - addMethods() Method
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