<?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>RentWiz Blog &#187; jquery event delegation</title>
	<atom:link href="http://getrentwiz.com/blog/tag/jquery-event-delegation/feed/" rel="self" type="application/rss+xml" />
	<link>http://getrentwiz.com/blog</link>
	<description>Rental Listings Made Easy.</description>
	<lastBuildDate>Sat, 25 Apr 2009 02:23:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How To: jQuery Event Delegation</title>
		<link>http://getrentwiz.com/blog/how-to-jquery-event-delegation/</link>
		<comments>http://getrentwiz.com/blog/how-to-jquery-event-delegation/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 01:32:52 +0000</pubDate>
		<dc:creator>Art Armstrong</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[bluetux]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jquery event delegation]]></category>
		<category><![CDATA[rentwiz]]></category>

		<guid isPermaLink="false">http://getrentwiz.com/blog/?p=57</guid>
		<description><![CDATA[Until now, I&#8217;ve used jQuery for some of its more basic attributes and features like tabs and animation with a lot of customization (which I am going to cover later). Recently I had to do some work involving adding, editing, and deleting files with some ajax to update the page on the fly. After running [...]]]></description>
			<content:encoded><![CDATA[<h3>Until now, I&#8217;ve used jQuery for some of its more basic attributes and features like tabs and animation with a lot of customization (which I am going to cover later). Recently I had to do some work involving adding, editing, and deleting files with some ajax to update the page on the fly. After running into the issue of jQuery animations not working after the update, I had to find a solution; that was Event Delegation.</h3>
<p>As much as I love jQuery and all its great uses I still have a lot to learn from it, and event delegation was one of those useful features. I have become fairly well versed in the animation field with interactions but until now had not dealt with an ajax update of a page that had items hidden by default  after it has loaded. The use for us was within the application section of the listings settings where the administrator is able to add, edit, or delete an application and the page would update the field showing them.</p>
<div class="mceTemp">
<dl id="attachment_69" class="wp-caption alignnone" style="width: 510px;">
<dt class="wp-caption-dt"><a rel="attachment wp-att-69" href="http://getrentwiz.com/blog/how-to-jquery-event-delegation/app-screenshot/"><img class="size-full wp-image-69" title="app-screenshot" src="http://getrentwiz.com/blog/wp/wp-content/uploads/2009/04/app-screenshot.jpg" alt="RentWiz Application Screenshot" width="500" height="135" /></a></dt>
</dl>
</div>
<p>Let&#8217;s get into some of the code and see what is actually happening. This is the html that is being manipulated&#8230;</p>
<pre>&lt;div id="current-applications"&gt;
	&lt;p&gt;
		&lt;a href="#" class="pdf-icon"&gt;My App&lt;/a&gt;
		&lt;a href="#" class="edit-link"&gt;Edit&lt;/a&gt; |
		&lt;a onclick='ajax_delete_app();' class="delete-link"&gt;Delete&lt;/a&gt;
	&lt;/p&gt;
	&lt;form name="myform" method="post" enctype="multipart/form-data"&gt;
	&lt;table class="edit-listings-box" cellpadding="0" cellspacing="0" border="0" width="100%"&gt;
		&lt;tr&gt;
			&lt;td&gt;
			&lt;!-- Information about application --&gt;
                        &lt;input type="submit" class="update-link" value=\"Update\" /&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
	&lt;/form&gt;
&lt;/div&gt;</pre>
<p>By default, the code above will hide the form and show the information only after the user clicks the application name &#8220;My App&#8221; or &#8220;Edit&#8221;. The &#8220;Delete&#8221; link will call some ajax functionality to delete the record and file attached to it; after which, it will redisplay all the information inside of the &lt;div id=&#8221;applications&#8221;&gt;&lt;/div&gt;. Now the event delegation comes in handy. Because the page information is reloading we need to use event delegation to always make the jQuery calls viable on the newly created output. The structure of the above html is used in the jQuery code to handle the animations even after a page update. The classes and ids need to be the same for the links and surrounding elements every time it is outputted so the event delegation can access the the correct segments.</p>
<pre>//jQuery Event Delegation
$('#applications').click(function (e) {
	if($(e.target).attr('class') == 'edit-link') {
		$(e.target).parent().next().contents().filter(".edit-listings-box").toggle("blind");
	}else if($(e.target).attr('class') == 'pdf-icon') {
		$(e.target).parent().next().contents().filter(".edit-listings-box").toggle("blind");
	}else if($(e.target).attr('class') == 'done-link') {
		$(e.target).parent().parent().parent().parent().toggle("blind");
	}else if($(e.target).attr('class') == 'update-link') {
		$(e.target).parent().parent().parent().parent().parent().submit();
        }else if($(e.target).attr('class') == 'delete-icon') {
		if($(e.target).parent().next().contents().filter(".edit-listings-box").is(':visible')) {
			$(e.target).parent().next().contents().filter(".edit-listings-box").hide("blind");
		}
	}

	return false;
});</pre>
<p>As you can observe, the jQuery code above will only deal with &lt;a&gt; tags inside the element with the id=&#8221;applications&#8221;. It will then check the class of the link and take the appropriate action. If you aren&#8217;t familiar with jQuery, the code referencing the element to hide may look odd but it is just  used to traverse the html. Anyway this is just a very basic idea of event delegation and how it was used within our web application. If you have any specific questions regarding the subject you are more than welcome to email me and ask. Take it easy!</p>
<p><a></a></p>
]]></content:encoded>
			<wfw:commentRss>http://getrentwiz.com/blog/how-to-jquery-event-delegation/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

