<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Enrique Delgado Monroy: Tag jquery</title>
    <link>http://www.enriquedelgado.com/articles/tag/jquery</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Work, Life, Stuff</description>
    <item>
      <title>Attaching functions to events in JavaScript: The unobstrusive way! - Part 2</title>
      <description>&lt;p&gt;&lt;a href="http://www.enriquedelgado.com/articles/2007/05/03/attaching-functions-to-events-in-javascript-the-unobstrusive-way"&gt;Previously&lt;/a&gt;, I talked about a good way to attach event listeners to &lt;span class="caps"&gt;DOM&lt;/span&gt; elements. I like the approach described in that post because is pure JavaScript and not dependent on a framework, but if you happen to be using a framework already, or are thinking on using some, here are two methods of achieving this task:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://prototypejs.org/api/event"&gt;Prototype.js&lt;/a&gt;: &lt;tt&gt;observe&lt;/tt&gt; method.&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;: &lt;tt&gt;bind&lt;/tt&gt; method and friends.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;It is important to remember that, your code should wait for the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree to completely load before attempting to attach events to elements. The typical scenario is that some JavaScript code is attempting to attach an event to an element that does not exist yet, because the document is still loading.&lt;/p&gt;


	&lt;p&gt;Each framework has ways of wrapping your code, so that it runs only when the &lt;span class="caps"&gt;DOM&lt;/span&gt; is ready:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;Prototype&lt;/strong&gt;&lt;br/&gt;&lt;pre style="overflow: auto; background: #EEE;"&gt;
document.observe("dom:loaded", function() {
  // Your code here
});
&lt;/pre&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;jQuery&lt;/strong&gt;&lt;br/&gt;&lt;pre style="overflow: auto; background: #EEE;"&gt;
$(document).ready(function(){
   // Your code here
 });

&lt;/pre&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Using Prototype&lt;/h3&gt;


	&lt;p&gt;Prototype offers several event handling methods, but the most common is &lt;code&gt;observe&lt;/code&gt; and &lt;code&gt;stopObserving&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Example:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$('foo').observe('click', respondToClick);

function respondToClick(event) {
  // This is how you access the element that triggered the event:
  var element = event.element();
  // Your code here, e.g.:
  alert('Hello');
}
&lt;/pre&gt;

	&lt;p&gt;Notice the name of the event, in this case is &lt;tt&gt;click&lt;/tt&gt;. The complete list of events are defined in the &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings"&gt;&lt;span class="caps"&gt;W3C&lt;/span&gt; recommendations&lt;/a&gt;, but here is a summary of the most common event names organized by type:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Mouse Events
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;click&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is clicked over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mousedown&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is pressed over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseup&lt;/tt&gt; &amp;#8211; Occurs when the pointing device button is released over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseover&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved onto an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mousemove&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved while it is over an element.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;mouseout&lt;/tt&gt; &amp;#8211; Occurs when the pointing device is moved away from an element.&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;Key Events
	&lt;ul&gt;
	&lt;li&gt;&lt;span class="caps"&gt;W3C&lt;/span&gt; does not have them, but the following are supplied by Prototype:&lt;br /&gt;&lt;tt&gt;&lt;span class="caps"&gt;KEY&lt;/span&gt;_BACKSPACE, &lt;span class="caps"&gt;KEY&lt;/span&gt;_TAB, &lt;span class="caps"&gt;KEY&lt;/span&gt;_RETURN, &lt;span class="caps"&gt;KEY&lt;/span&gt;_ESC, &lt;span class="caps"&gt;KEY&lt;/span&gt;_LEFT, &lt;span class="caps"&gt;KEY&lt;/span&gt;_UP, &lt;span class="caps"&gt;KEY&lt;/span&gt;_RIGHT, &lt;span class="caps"&gt;KEY&lt;/span&gt;_DOWN, &lt;span class="caps"&gt;KEY&lt;/span&gt;_DELETE, &lt;span class="caps"&gt;KEY&lt;/span&gt;_HOME, &lt;span class="caps"&gt;KEY&lt;/span&gt;_END, &lt;span class="caps"&gt;KEY&lt;/span&gt;_PAGEUP, &lt;span class="caps"&gt;KEY&lt;/span&gt;_PAGEDOWN&lt;/tt&gt;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;&lt;span class="caps"&gt;HTML&lt;/span&gt; Events
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;load&lt;/tt&gt; &amp;#8211; Occurs when the &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation finishes loading.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;unload&lt;/tt&gt; &amp;#8211; Occurs when the &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation removes a document from a window or frame.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;select&lt;/tt&gt; &amp;#8211; Occurs when a user selects some text in a text field.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;change&lt;/tt&gt; &amp;#8211; Occurs when a control loses the input focus and its value has been modified since gaining focus.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;submit&lt;/tt&gt; &amp;#8211; Occurs when a form is submitted.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;focus&lt;/tt&gt; &amp;#8211; Occurs when an element receives focus either via a pointing device or by tabbing navigation.&lt;/li&gt;
		&lt;li&gt;&lt;tt&gt;blur&lt;/tt&gt; &amp;#8211; Occurs when an element loses focus either via the pointing device or by tabbing navigation.&lt;/li&gt;
	&lt;/ul&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;Using jQuery&lt;/h3&gt;


	&lt;p&gt;jQuery mostly has &amp;#8220;helper methods&amp;#8221; to set event listeners that are named after the event name, but the two most basic methods are &lt;tt&gt;bind&lt;/tt&gt; and &lt;tt&gt;unbind&lt;/tt&gt;.&lt;/p&gt;


	&lt;p&gt;Example:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$("#foo").bind("click", function(e){
  // This is how you access the element that triggered the event:
  var element = event.target;
  // Your code here, e.g.:
  alert('Hello');
});
&lt;/pre&gt;

Again, notice the name of the event, in this case is &lt;tt&gt;click&lt;/tt&gt;. According to the jQuery documentation, these are the allowed event names:
	&lt;ul&gt;
	&lt;li&gt;&lt;tt&gt;blur, focus, load, resize, scroll, unload, click, dblclick,  mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select,  submit, keydown, keypress, keyup, error&lt;/tt&gt;.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Most helper elements are named after the event names above, so you have things like &lt;tt&gt;click()&lt;/tt&gt;, &lt;tt&gt;focus()&lt;/tt&gt;, &lt;tt&gt;submit()&lt;/tt&gt;, etc.&lt;/p&gt;


	&lt;p&gt;Example using an anonymous function as the functioned to be executed when the event is triggered:&lt;/p&gt;


&lt;pre style="overflow: auto; background: #EEE;"&gt;
$("#foo").click(function () { 
  // The element is access by using "$(this)" 
  // Your code here, e.g.:
  alert('Hello');
});
&lt;/pre&gt;

	&lt;h3&gt;Event Delegation&lt;/h3&gt;


	&lt;p&gt;One often source of confusion is how to attach event listeners to dynamically-generated &lt;span class="caps"&gt;DOM&lt;/span&gt; elements (for example, adding &lt;tt&gt;&amp;lt;li&amp;gt;&lt;/tt&gt; elements to a list via &lt;span class="caps"&gt;AJAX&lt;/span&gt;).&lt;/p&gt;


	&lt;p&gt;When an element is crated, it is not already bound to an event listener, so you will have to perform a two-step process: create the element in the &lt;span class="caps"&gt;DOM&lt;/span&gt;, and then attach the event listener. This can get tedious easily, so instead of attaching the event listener to the &lt;span class="caps"&gt;DOM&lt;/span&gt; element desired, you attach an event listener to a parent element in the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree.&lt;/p&gt;


	&lt;p&gt;In turn, this parent element acts as a &amp;#8220;catch all&amp;#8221; event listener. It will figure out which element triggered the event in the first place and act accordingly. This technique is referred to as &lt;a href="http://icant.co.uk/sandbox/eventdelegation/"&gt;event delegation&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Event delegation works because some events &amp;#8220;bubble up&amp;#8221; from child nodes to parent nodes in the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree; see &lt;a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling"&gt;event bubbling&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Hope thins helps someone out there getting started. Post your questions if you run into trouble. :)&lt;/p&gt;</description>
      <pubDate>Fri, 24 Oct 2008 08:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3690a8d5-8f49-4316-95b8-250cc37c4f3f</guid>
      <author>Enrique Delgado</author>
      <link>http://www.enriquedelgado.com/articles/2008/10/24/attaching-functions-to-events-in-javascript-the-unobstrusive-way-part-2</link>
      <category>Web Development</category>
      <category>javascript</category>
      <category>html</category>
      <category>DOM</category>
      <category>prototype</category>
      <category>jquery</category>
    </item>
  </channel>
</rss>
