<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tomputer &#187; events</title>
	<atom:link href="http://droza.net/blog/tag/events/feed/" rel="self" type="application/rss+xml" />
	<link>http://droza.net/blog</link>
	<description>Software. Gadgets. Music. Rants. By Tom D\&#039;Roza</description>
	<lastBuildDate>Thu, 23 Dec 2010 13:26:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Custom events in JQuery</title>
		<link>http://droza.net/blog/2009/01/25/custom-events-in-jquery/</link>
		<comments>http://droza.net/blog/2009/01/25/custom-events-in-jquery/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 18:49:54 +0000</pubDate>
		<dc:creator>tdroza</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://droza.net/blog/?p=129</guid>
		<description><![CDATA[For a project at work this week I needed to modify a Javascript library so that under a particular condition it would generate a JS event that could be handled elsewhere in the application.  Why use JS events rather than just call a regular JS function? Well, I guess partly to maintain consistency with the [...]]]></description>
			<content:encoded><![CDATA[
<!-- wp-jquery-lightbox, a WordPress plugin by ulfben --> 
<p>For a project at work this week I needed to modify a Javascript library so that under a particular condition it would generate a JS event that could be handled elsewhere in the application.  Why use JS events rather than just call a regular JS function? Well, I guess partly to maintain consistency with the way that other events are handled within the app, and partly because it&#8217;s a more elegant solution: When an event is thrown, if there&#8217;s nothing to catch and handle the event it doesn&#8217;t matter but when you call a JS function you need to be sure that the function exists otherwise you&#8217;ll end up with JS errors on the page so you have to first test that the function exists before you call it. Also an event can <a href="http://www.quirksmode.org/js/events_order.html">propagate</a> up through the ancestory of objects on your page so it can be handled at whichever level is most appropriate or handled in different ways at different levels.</p>
<p>In JQuery the syntax for creating an event handler is exremely simple, for example, given the following HTML:</p>
<pre class="brush: jscript; title: ;">
&lt;div id='container'&gt;
   Click me!
&lt;/div&gt;
</pre>
<p>&#8230;the JQuery code to handle for the click event would be:</p>
<pre class="brush: jscript; title: ;">
$('#container').click(function() {
   alert('You clicked me!');
});
</pre>
<p>In the above example, the &#8220;click&#8221; method is a shorthand utility method provided by JQuery, but is the same as:</p>
<pre class="brush: jscript; title: ;">
$('#container').bind('click', function(e) {
   alert('You clicked me!');
});
</pre>
<p>There&#8217;s a whole bunch of <a title="Event reference on W3Schools" href="http://www.w3schools.com/jsref/jsref_events.asp" target="_blank">standard HTML events</a> that the browser may generate in response to user input (and that can be handled in similar fashion). You can also get JQuery to generate these events programatically.</p>
<p>It turns out that you can create a handler for your own custom event in exactly the same way, just change the name of the event (&#8220;click&#8221; in the example above), to the name of the event you want to create and be sure to use the longhand &#8220;bind&#8221; method. E.g.</p>
<pre class="brush: jscript; title: ;">
$('#container').bind('myEvent', function(e) {
   alert('My event just fired');
});
</pre>
<p>Then, when you want to trigger this event, use the err&#8230; &#8220;trigger&#8221; function:</p>
<pre class="brush: jscript; title: ;">
   $('#container').trigger('myEvent');
</pre>
<p>Because we&#8217;re using events rather than a straight method call (and JQuery 1.3 or later) we could write another handler for myEvent but on an ancestor of #container &#8211; the document body in this case. The handlers would be called in order, first on #container, then on the body. If we want to prevent the event from bubbling up through the DOM we can kill it with the <a title="See the JQuery Event documentation" href="http://docs.jquery.com/Events/jQuery.Event#event.stopPropagation.28.29" target="_blank">stopPropagation()</a> function:</p>
<pre class="brush: jscript; title: ;">
$('#container').bind('myEvent', function(e) {
   e.stopPropagation();
   alert('My event just fired');
});
</pre>
<p>I was surprised by just how simple this was once I realised that the process for custom events is exactly the same as that for standard browser events.</p>

<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post?url=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
<a href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fdroza.net%2Fblog%2F2009%2F01%2F25%2Fcustom-events-in-jquery%2F&amp;title=Custom+events+in+JQuery" title="Stumble it!"><img src="http://www.stumbleupon.com/favicon.ico" width="16" height="16" alt="[StumbleUpon]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://droza.net/blog/2009/01/25/custom-events-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

