Prototype - fire() Method



This method is used to fire a custom event with the current element as its target.

The custom event has all the same properties and methods of native events. Like a native event, it will bubble up through the DOM unless its propagation is explicitly stopped.

Custom events are dispatched synchronously: Element#fire waits until the event finishes its life cycle, then returns the event itself.

Syntax

element.fire(eventName[, memo]);

The optional second argument will be assigned to the memo property of the event object so that it can be read by event handlers.

Return Value

It returns custom event

Example

In this example, an element with ID (firstDiv) frobbed widget #19.

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         document.observe("widget:frobbed", function(event) {
            alert("Element with ID (" + event.target.id +
               ") frobbed widget #" + event.memo.widgetNumber + ".");
         });
         function showResult() {
            someNode = $('firstDiv');
            someNode.fire("widget:frobbed", { widgetNumber: 19 });
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      
      <div id = "firstDiv">
         <p>This is first paragraph</p> 
      </div>
      <br />
      
      <input type = "button" value = "showResult" onclick = "showResult();"/>
   </body>
</html>

Output

prototype_element_object.htm
Advertisements