<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - Event</title>
  <id>tag:www.prototypejs.org,2008:mephisto/api/event</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.prototypejs.org/feed/api/event/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.prototypejs.org/api/event" rel="alternate" type="text/html"/>
  <updated>2007-12-05T01:26:05Z</updated>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2007-12-05:18125</id>
    <published>2007-12-05T01:22:00Z</published>
    <updated>2007-12-05T01:26:05Z</updated>
    <category term="Event"/>
    <category term="1.6"/>
    <link href="http://www.prototypejs.org/api/event/extend" rel="alternate" type="text/html"/>
    <title>extend</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.extend(event)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Extends &lt;code&gt;event&lt;/code&gt; with all of the methods contained in &lt;code&gt;Event.Methods&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.extend(event)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Extends &lt;code&gt;event&lt;/code&gt; with all of the methods contained in &lt;code&gt;Event.Methods&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that all events inside handlers that were registered using &lt;a href=&quot;/api/event/observe&quot;&gt;&lt;code&gt;Event.observe&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;/api/element/observe&quot;&gt;&lt;code&gt;Element#observe&lt;/code&gt;&lt;/a&gt; will be extended automatically.&lt;/p&gt;

&lt;p&gt;You need only call &lt;code&gt;Event.extend&lt;/code&gt; manually if you register a handler a different way (e.g., the &lt;code&gt;onclick&lt;/code&gt; attribute). We really can&#8217;t encourage that sort of thing, though.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-08:13007</id>
    <published>2007-01-08T17:35:00Z</published>
    <updated>2007-11-11T17:38:41Z</updated>
    <category term="Event"/>
    <category term="1.6"/>
    <link href="http://www.prototypejs.org/api/event/unloadCache" rel="alternate" type="text/html"/>
    <title>unloadCache</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.unloadCache()&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unregisters all event handlers registered through &lt;a href=&quot;/api/event/observe&quot;&gt;&lt;code&gt;observe&lt;/code&gt;&lt;/a&gt;.  Automatically wired for you. &lt;strong&gt;Not available since 1.6.&lt;/strong&gt;&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.unloadCache()&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unregisters all event handlers registered through &lt;a href=&quot;/api/event/observe&quot;&gt;&lt;code&gt;observe&lt;/code&gt;&lt;/a&gt;.  Automatically wired for you. &lt;strong&gt;Not available since 1.6.&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;deprecated&quot;&gt;
  Backwards incompatibility notice:&lt;br /&gt;
  This method is removed in the &lt;b&gt;1.6&lt;/b&gt; release. Remove calls to it from any script you currently use.
&lt;/div&gt;

&lt;h3&gt;The sad tale of MSIE, event handlers and memory leaks&lt;/h3&gt;

&lt;p&gt;There is a significant issue with MSIE, which is that under a variety of conditions, it just will not release event handlers when the page unloads.  These handlers will stay in RAM, filling it up slowly, clogging the browser&#8217;s arteries.  This is known as a memory leak.&lt;/p&gt;

&lt;p&gt;Of course, manually keeping tabs on all the handlers you register (which you do through &lt;a href=&quot;/api/event/observe&quot;&gt;&lt;code&gt;observe&lt;/code&gt;&lt;/a&gt;, being such a nice person) is pretty tedious.  And boring.  It would be, in short, the essence of un-cool.&lt;/p&gt;

&lt;p&gt;Which is why Prototype takes care of it for you.  It keeps tabs, and when &lt;code&gt;unloadCache&lt;/code&gt; is called, it unregisters everything and frees references, which is akin to sending a big pink lavender-perfumed invitation to the garbage collector.&lt;/p&gt;

&lt;h3&gt;You don&#8217;t even need to know this&lt;/h3&gt;

&lt;p&gt;What&#8217;s even better is, Prototype automatically hooks &lt;code&gt;unloadCache&lt;/code&gt; to page unloading, exclusively for MSIE.  So you don&#8217;t have anything to do.  It&#8217;s all taken care of.  We just though you&#8217;d like to know.  Go do something productive, some value-added JavaScript code for instance.  We&#8217;re not here to hinder you with automatable details.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Justin</name>
    </author>
    <id>tag:www.prototypejs.org,2006-12-05:12745</id>
    <published>2006-12-05T21:44:00Z</published>
    <updated>2007-12-05T01:11:45Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event" rel="alternate" type="text/html"/>
    <title>Event</title>
<content type="html">
            &lt;h3&gt;&#9834; What a wonderful mess (it would be) &#9835;&lt;/h3&gt;

&lt;p&gt;Event management is one of the really sore spots of cross-browser scripting.&lt;/p&gt;

&lt;p&gt;True, the prominent issue is: everybody does it the W3C way, and MSIE does it another way altogether.  But there are quite a few subtler, sneakier issues here and there waiting to bite your ankle, such as the &lt;code&gt;keypress&lt;/code&gt;/&lt;code&gt;keydown&lt;/code&gt; issue with KHTML-based browsers (Konqueror and Safari).  Also, MSIE has a tendency to leak memory when it comes to discarding event handlers.&lt;/p&gt;

&lt;h3&gt;Prototype to the rescue!&lt;/h3&gt;

&lt;p&gt;Of course, Prototype smooths it over so well you&#8217;ll forget these troubles even exist.  Enter the &lt;code&gt;Event&lt;/code&gt; namespace.  It is replete with methods (listed at the top and bottom of this page), that all take the current event object as an argument, and happily produce the information you&#8217;re requesting, across all major browsers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Event&lt;/code&gt; also provides a standardized list of key codes you can use with keyboard-related events.  The following constants are defined in the namespace: &lt;code&gt;KEY_BACKSPACE&lt;/code&gt;, &lt;code&gt;KEY_TAB&lt;/code&gt;, &lt;code&gt;KEY_RETURN&lt;/code&gt;, &lt;code&gt;KEY_ESC&lt;/code&gt;, &lt;code&gt;KEY_LEFT&lt;/code&gt;, &lt;code&gt;KEY_UP&lt;/code&gt;, &lt;code&gt;KEY_RIGHT&lt;/code&gt;, &lt;code&gt;KEY_DOWN&lt;/code&gt;, &lt;code&gt;KEY_DELETE&lt;/code&gt;, &lt;code&gt;KEY_HOME&lt;/code&gt;, &lt;code&gt;KEY_END&lt;/code&gt;, &lt;code&gt;KEY_PAGEUP&lt;/code&gt;, &lt;code&gt;KEY_PAGEDOWN&lt;/code&gt;. The names are self-explanatory.&lt;/p&gt;

&lt;p&gt;The functions you&#8217;re most likely to use a lot are &lt;a href=&quot;event/observe&quot;&gt;&lt;code&gt;observe&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;event/element&quot;&gt;&lt;code&gt;element&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;event/stop&quot;&gt;&lt;code&gt;stop&lt;/code&gt;&lt;/a&gt;.  As for the others, your mileage may vary; it&#8217;s all about what your web app does.&lt;/p&gt;

&lt;h3&gt;Instance methods on event objects &lt;div class=&quot;box&quot;&gt;1.6&lt;/div&gt;&lt;/h3&gt;

&lt;p&gt;As of Prototype 1.6, all methods on the &lt;code&gt;Event&lt;/code&gt; object are now also available as instance methods on the event object itself:&lt;/p&gt;

&lt;h4&gt;Before&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$('foo').observe('click', respondToClick);

function respondToClick(event) {
  var element = Event.element(event);
  element.addClassName('active');
}&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;After&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$('foo').observe('click', respondToClick);

function respondToClick(event) {
  var element = event.element();
  element.addClassName('active');
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These methods are added to the event object through &lt;a href=&quot;/api/event/extend&quot;&gt;&lt;code&gt;Event.extend&lt;/code&gt;&lt;/a&gt;, in the same way that &lt;code&gt;Element&lt;/code&gt; methods are added to DOM nodes through &lt;a href=&quot;/api/element/extend&quot;&gt;&lt;code&gt;Element.extend&lt;/code&gt;&lt;/a&gt;. Events are extended automatically when handlers are registered with Prototype&#8217;s &lt;a href=&quot;/api/event/observe&quot;&gt;&lt;code&gt;Event.observe&lt;/code&gt;&lt;/a&gt; method; if you&#8217;re using a different method of event registration, for whatever reason, you&#8217;ll need to extend these events manually with &lt;code&gt;Event.extend&lt;/code&gt;. &lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12504</id>
    <published>2006-11-18T21:14:00Z</published>
    <updated>2007-01-10T22:38:46Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/pointerY" rel="alternate" type="text/html"/>
    <title>pointerY</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.pointerY(event) -&gt; Number&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the absolute vertical position for a mouse event.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.pointerY(event) -&gt; Number&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the absolute vertical position for a mouse event.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: the position is absolute on the &lt;em&gt;page&lt;/em&gt;, not on the &lt;em&gt;viewport&lt;/em&gt;: scrolling down increases the returned value for events on the same viewport location.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12505</id>
    <published>2006-11-18T21:14:00Z</published>
    <updated>2007-01-10T21:09:51Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/stop" rel="alternate" type="text/html"/>
    <title>stop</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.stop(event)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Stops the event&#8217;s propagation and prevents its default action from being triggered eventually.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.stop(event)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Stops the event&#8217;s propagation and prevents its default action from being triggered eventually.&lt;/p&gt;
&lt;p&gt;There are two aspects to how your browser handles an event once it fires up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The browser usually triggers event handlers on the actual element the event occurred on, then on its parent element, and so on and so forth, until the document&#8217;s root element is reached.  This is called &lt;em&gt;event bubbling&lt;/em&gt;, and is the most common form of event propagation.  You may very well want to stop this propagation when you just handled an event, and don&#8217;t want it to keep bubbling up (or see no need for it).&lt;/li&gt;
&lt;li&gt;Once your code got a chance to process the event, the browser handles it as well, if that event has a &lt;em&gt;default behavior&lt;/em&gt;.  For instance, clicking on links navigates to them; submitting forms sends them over to the server side; hitting the Return key in a single-line form field submits it; etc.  You may very well want to prevent this default behavior if you do your own handling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because stopping one of those aspects means, in 99.9% of the cases, preventing the other one as well, Prototype bundles both in this &lt;code&gt;stop&lt;/code&gt; function.  Calling it on
 an event object stop propagation &lt;em&gt;and&lt;/em&gt; prevents the default behavior.&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;Here&#8217;s a simple code that prevents a form from being sent to the server side if a certain field is empty.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe('signinForm', 'submit', function(event) {
  var login = $F('login').strip();
  if ('' == login) {
    Event.stop(event);
    // Display the issue one way or another
  }
});
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12506</id>
    <published>2006-11-18T21:14:00Z</published>
    <updated>2007-01-10T23:00:29Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/findElement" rel="alternate" type="text/html"/>
    <title>findElement</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.findElement(event, tagName) -&gt; Element&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the first DOM element with a given tag name, upwards from the one on which the event occurred.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.findElement(event, tagName) -&gt; Element&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the first DOM element with a given tag name, upwards from the one on which the event occurred.&lt;/p&gt;
&lt;p&gt;Sometimes, you&#8217;re not interested in the actual element that got hit by the event.  Sometimes you&#8217;re interested in its &#8220;closest element,&#8221; (either the original one, or its container, or its container&#8217;s container, etc.), defined by its tag (e.g., &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;).  This is what &lt;code&gt;findElement&lt;/code&gt; is for.&lt;/p&gt;

&lt;p&gt;The provided tag name will be compared in a case-insensitive manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If no matching element is found, the document itself (&lt;code&gt;HTMLDocument&lt;/code&gt; node) is returned.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;Here&#8217;s a simple code that lets you click everywhere on the page and hides the closest-fitting paragraph around your click (if any).&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe(document.body, 'click', function(event) {
  var elt = Event.findElement(event, 'P');
  if (elt != document)
    $(elt).hide();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more complex searches, you&#8217;ll need to get the actual element and use &lt;a href=&quot;/api/element/methods/up&quot;&gt;&lt;code&gt;up&lt;/code&gt;&lt;/a&gt; on it, which lets you express your request with CSS syntax, and also search farther than the first match (plus, the result is extension-guaranteed):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe(document.body, 'click', function(event) {
  // First element from event source with 'container' among its CSS classes
  var elt = $(Event.element(event)).up('.container');
  // Or: second DIV from the event source
  // elt = $(Event.element(event)).up('div', 1);
  // Or: second DIV with 'holder' among its CSS classes...
  // elt = $(Event.element(event)).up('div.holder', 1);
  elt.hide();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;If you&#8217;re looking for the element directly hit by the event, just use the &lt;a href=&quot;/api/event/element&quot;&gt;&lt;code&gt;element&lt;/code&gt;&lt;/a&gt; function.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12502</id>
    <published>2006-11-18T21:13:00Z</published>
    <updated>2007-01-10T22:42:11Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/isLeftClick" rel="alternate" type="text/html"/>
    <title>isLeftClick</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.isLeftClick(event) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Determines whether a button-related mouse event was about the &#8220;left&#8221; (primary, actually) button.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.isLeftClick(event) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Determines whether a button-related mouse event was about the &#8220;left&#8221; (primary, actually) button.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: this is not an absolute left, but &#8220;left for default&#8221; (right-handed).  On systems configured for left-handed users, where the primary button is the right one (from an absolute perspective), this function examines the proper button.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12503</id>
    <published>2006-11-18T21:13:00Z</published>
    <updated>2007-01-10T22:39:27Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/pointerX" rel="alternate" type="text/html"/>
    <title>pointerX</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.pointerX(event) -&gt; Number&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the absolute horizontal position for a mouse event.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.pointerX(event) -&gt; Number&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the absolute horizontal position for a mouse event.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: the position is absolute on the &lt;em&gt;page&lt;/em&gt;, not on the &lt;em&gt;viewport&lt;/em&gt;: scrolling right increases the returned value for events on the same viewport location.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12500</id>
    <published>2006-11-18T21:12:00Z</published>
    <updated>2007-01-16T04:55:05Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/stopObserving" rel="alternate" type="text/html"/>
    <title>stopObserving</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.stopObserving(element, eventName, handler[, useCapture = false])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unregisters an event handler.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.stopObserving(element, eventName, handler[, useCapture = false])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unregisters an event handler.&lt;/p&gt;
&lt;p&gt;This function is called with exactly the same argument semantics as &lt;a href=&quot;/api/event/observe&quot;&gt;&lt;code&gt;observe&lt;/code&gt;&lt;/a&gt;.  It unregisters an event handler, so the handler is not called anymore for this element+event pair.&lt;/p&gt;

&lt;h3&gt;Why won't it stop observing!?&lt;/h3&gt;

&lt;p&gt;For &lt;code&gt;stopObserving&lt;/code&gt; to work, you must pass &lt;em&gt;exactly the same arguments&lt;/em&gt; as those you did to the corresponding &lt;code&gt;observe&lt;/code&gt; call.  Complying with this seems straightforward enough, but there is a common pattern where code is not what it seems to be:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var obj = {
  &#8230;
  fx: function(event) {
    &#8230;
  }
};

Event.observe(elt, 'click', obj.fx.bindAsEventListener(obj));
&#8230;

// THIS IS WRONG, DON'T DO IT!
Event.stopObserving(elt, 'click', obj.fx.bindAsEventListener(obj)); // Won't work!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here, although it may seem fine at first glance, you must remember that &lt;a href=&quot;/api/function/bindAsEventListener&quot;&gt;&lt;code&gt;bindAsEventListener&lt;/code&gt;&lt;/a&gt; returns a fresh anonymous function that wraps your method.  This means that every call to it returns a new function.  Therefore, the code above requests stopping on another function than was used when setting up observation.  No match is found, and the original observer is left untroubled.&lt;/p&gt;

&lt;p&gt;To avoid this, you need to &quot;cache&quot; the bound functions (which is, for instance, what &lt;a href=&quot;http://script.aculo.us&quot;&gt;script.aculo.us&lt;/a&gt; does in many of its classes), like th
is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var obj = {
  &#8230;
  fx: function(event) {
    &#8230;
  },
};

obj.bfx = obj.fx.bindAsEventListener(obj);

Event.observe(elt, 'click', obj.bfx);
&#8230;
Event.stopObserving(elt, 'click', obj.bfx);
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;/api/event/unloadCache&quot;&gt;&lt;code&gt;unloadCache&lt;/code&gt;&lt;/a&gt; function is related and worth a look.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12501</id>
    <published>2006-11-18T21:12:00Z</published>
    <updated>2007-04-28T05:29:11Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/element" rel="alternate" type="text/html"/>
    <title>element</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.element(event) -&gt; Element&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the DOM element on which the event occurred.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.element(event) -&gt; Element&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the DOM element on which the event occurred.&lt;/p&gt;
&lt;p&gt;Note that from v1.5.1, the element returned by &lt;code&gt;Event.element&lt;/code&gt; is already extended.&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;Here&#8217;s a simple code that lets you click everywhere on the page and, if you click directly on paragraphs, hides them.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe(document.body, 'click', function(event) {
  var element = Event.element(event);
  if ('P' == element.tagName)
    element.hide();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;There is a subtle distinction between this function and &lt;a href=&quot;/api/event/findElement&quot;&gt;&lt;code&gt;findElement&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Note for Prototype 1.5.0&lt;/h3&gt;

&lt;p&gt;Note that prior to version 1.5.1, if the browser does not support &lt;em&gt;native DOM extensions&lt;/em&gt; (see &lt;a href=&quot;/learn/extensions&quot;&gt;this page&lt;/a&gt; for further details), the element returned by &lt;code&gt;Event.element&lt;/code&gt; might very well &lt;strong&gt;not be extended&lt;/strong&gt;.  If you intend to use methods from &lt;a href=&quot;/api/element/methods&quot;&gt;&lt;code&gt;Element.Methods&lt;/code&gt;&lt;/a&gt; on it, you need to wrap the call in the &lt;a href=&quot;/api/utility/dollar&quot;&gt;&lt;code&gt;$()&lt;/code&gt;&lt;/a&gt; function like so:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe(document.body, 'click', function(event) {
  var element = $(Event.element(event));
  /* ...  */
});
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:www.prototypejs.org,2006-11-18:12499</id>
    <published>2006-11-18T21:10:00Z</published>
    <updated>2007-07-09T22:43:07Z</updated>
    <category term="Event"/>
    <link href="http://www.prototypejs.org/api/event/observe" rel="alternate" type="text/html"/>
    <title>observe</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.observe(element, eventName, handler[, useCapture = false])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Registers an event handler on a DOM element.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Event.observe(element, eventName, handler[, useCapture = false])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Registers an event handler on a DOM element.&lt;/p&gt;
&lt;h3&gt;An important note&lt;/h3&gt;

&lt;p&gt;First, if you're coming from a background where you'd use HTML event attributes (e.g. &lt;code&gt;&amp;lt;body onload=&quot;return myFunction()&quot;&amp;gt;&lt;/code&gt;) or DOM Level-0 event properties (e.g. &lt;code&gt;window.onload = myFunction;&lt;/code&gt;), you must shed those vile ways :-) and understand what &lt;code&gt;observe&lt;/code&gt; does.&lt;/p&gt;

&lt;p&gt;It does not &lt;em&gt;replace&lt;/em&gt; existing handlers for that same element+event pair.  It &lt;em&gt;adds&lt;/em&gt; to the &lt;em&gt;list of handlers&lt;/em&gt; for that pair.  Using &lt;code&gt;observe&lt;/code&gt; will never incapacitate earlier calls.&lt;/p&gt;

&lt;h3&gt;What are those arguments about?&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The DOM element you want to observe; as always in Prototype, this can be either an actual DOM reference, or the ID string for the element.&lt;/li&gt;
&lt;li&gt;The standardized event name, as per the DOM level supported by your browser (usually &lt;a href=&quot;http://www.w3.org/TR/DOM-Level-2-Events/events.html&quot;&gt;DOM Level 2 Events&lt;/a&gt;, see section 1.6 for event names and details).  This can be as simple as &lt;code&gt;'click'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The handler function.  This can be an anonymous function you create on-the-fly, a vanilla function, a &lt;a href=&quot;/api/function/bindAsEventListener&quot;&gt;bound event listener&lt;/a&gt;, it's up to you.&lt;/li&gt;
&lt;li&gt;Optionally, you can request &lt;em&gt;capturing&lt;/em&gt; instead of &lt;em&gt;bubbling&lt;/em&gt;.  The details are in the DOM spec referred to above.  Note that capturing is not supported on several major browsers, and is seldom what you need, anyway.  Most often, you won't even provide this argument.                                                                                                                                                                                                                              &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;The requirement people too often forget&#8230;&lt;/h3&gt;

&lt;p&gt;To register a function as an event handler, the DOM element that you want to observe &lt;strong&gt;must already exist&lt;/strong&gt; in the DOM (in other words, it must have appeared in the source, or been dynamically created and inserted, before your handler-registration script line runs).&lt;/p&gt;

&lt;h3&gt;A simple example&lt;/h3&gt;

&lt;p&gt;Let us assume the following (X)HTML fragment:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;
&amp;lt;form id=&quot;signinForm&quot; method=&quot;post&quot; action=&quot;/auth/signin&quot;&amp;gt;
&#8230;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here's how to register your function &lt;code&gt;checkForm&lt;/code&gt; on form submission:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe('signinForm', 'submit', checkForm);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, you'd want this line of code to run once the form exists in the DOM; but putting inline scripts in the document is pretty obtrusive, so instead we'll go for a simple approach that waits till the page is fully loaded:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Event.observe(window, 'load', function() {
  Event.observe('signinForm', 'submit', checkForm);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Just a little wrapping&#8230;&lt;/p&gt;

&lt;p&gt;Note that if your page is heavy, you might want to run this code before the page is fully loaded: just wait until the DOM is loaded, that will be enough.  There is currently no standard event for this, but &lt;a href=&quot;http://tanny.ica.com/ica/tko/tkoblog.nsf/dx/domcontentloaded-event-for-browsers&quot;&gt;here is a helpful article&lt;/a&gt; you can use.&lt;/p&gt;

&lt;h3&gt;The tricky case of methods that need &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Passing your event handler as a function argument, you lose its &lt;em&gt;binding&lt;/em&gt;.  That is, you lose its ability to know what &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; means to the original function.  If you're passing in a method that does need to use the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; reference (for instance, to access fields of its container object), you're in trouble.&lt;/p&gt;

&lt;p&gt;Or not.&lt;/p&gt;

&lt;p&gt;This is an issue specifically addressed by Prototype's &lt;a href=&quot;/api/function/bindAsEventListener&quot;&gt;&lt;code&gt;bindAsEventListener&lt;/code&gt;&lt;/a&gt; function.  Check it out if you don't know it already.  Usage is simple:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var Checks = {
  // some stuff our 'generic' function needs

  generic: function(event) {
    // Some generic, all-purpose checking (e.g. empty required fields)
  }
};

Event.observe('signinForm', 'submit', Checks.generic.bindAsEventListener(Checks));
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;/api/event/stopObserving&quot;&gt;&lt;code&gt;stopObserving&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;/api/event/unloadCache&quot;&gt;&lt;code&gt;unloadCache&lt;/code&gt;&lt;/a&gt; methods are closely related, and worth a look.&lt;/p&gt;
          </content>  </entry>
</feed>
