Tutorials Point


  Learn Prototype
  Prototype Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Prototype fire() Method


previous next AddThis Social Bookmark Button


Advertisements

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 custome event

Example:

In this exmaple 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>

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



previous next Printer Friendly

Advertisements


  

Advertisements