<?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>Learning jQuery &#187; Advanced</title>
	<atom:link href="http://www.learningjquery.com/category/levels/advanced/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.com</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Thu, 26 Aug 2010 16:55:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making a jQuery Plugin Truly Customizable</title>
		<link>http://www.learningjquery.com/2009/03/making-a-jquery-plugin-truly-customizable</link>
		<comments>http://www.learningjquery.com/2009/03/making-a-jquery-plugin-truly-customizable#comments</comments>
		<pubDate>Wed, 11 Mar 2009 20:53:42 +0000</pubDate>
		<dc:creator>James Padolsey</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=641</guid>
		<description><![CDATA[Most if not all of the jQuery plugins out there have some level of customization. But very few of the plugin authors have mastered the very particular art involved.

Achieving the "optimum level" of customization is a bit of a balancing act&#8230; go too far either way and you've got an unusable plugin!

Bob and Sue

Let's say [...]]]></description>
			<content:encoded><![CDATA[<p>Most if not all of the jQuery plugins out there have some level of customization. But very few of the plugin authors have mastered the very particular art involved.</p>

<p>Achieving the "optimum level" of customization is a bit of a balancing act&hellip; go too far either way and you've got an unusable plugin!</p>
<span id="more-641"></span>
<h4>Bob and Sue</h4>

<p>Let's say Bob has created a wicked new gallery plugin (called "superGallery") which takes a list of images and makes them navigable. Bob's thrown in some animation to make it more interesting. He's tried to make the plugin as customizable as possible, and has ended up with something like this:</p>

<div class="igBar"><span id="ljavascript-9"><a href="#" onclick="javascript:showPlainTxt('javascript-9'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-9">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">superGallery</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Bob's default settings:</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; textColor <span style="color: #339933;">:</span> <span style="color: #3366CC;">'#000'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; backgroundColor <span style="color: #339933;">:</span> <span style="color: #3366CC;">'#FFF'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; fontSize <span style="color: #339933;">:</span> <span style="color: #3366CC;">'1em'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; delay <span style="color: #339933;">:</span> <span style="color: #3366CC;">'quite long'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; getTextFromTitle <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; getTextFromRel <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; getTextFromAlt <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; animateWidth <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; animateOpacity <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; animateHeight <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; animationDuration <span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">500</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; clickImgToGoToNext <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; clickImgToGoToLast <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; nextButtonText <span style="color: #339933;">:</span> <span style="color: #3366CC;">'next'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; previousButtonText <span style="color: #339933;">:</span> <span style="color: #3366CC;">'previous'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; nextButtonTextColor <span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; previousButtonTextColor <span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> settings <span style="color: #339933;">=</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ----------------------------</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Plugin code would go here...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ----------------------------</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The first thing that probably comes to your mind (ok, maybe not the first) is the prospect of how huge this plugin must be to accommodate such a level of customization. The plugin, if it weren't fictional, would probably be a lot larger than necessary. There are only so many kilobytes people will be willing to spend!</p>

<p>Now, our friend Bob thinks this is all fine; in fact, he's quite impressed with the plugin and its level of customization. He believes that all the options make for a more versatile solution, one which can be used in many different situations.</p>

<p>Sue, another friend of ours, has decided to use this new plugin. She has set up all of the options required and now has a working solution sitting in front of her. It's only five minutes later, after playing with the plugin, that she realizes the gallery would look much nicer if each image's width were animated at a slower speed. She hastily searches through Bob's documentation but finds no <code>animateWidthDuration</code> option!</p>

<p>Do you see the problem?</p>

<p>It's not really about how many options your plugin has; but <em>what</em> options it has!</p>

<p>Bob has gone a little over the top. The level of customization he's offering, while it may seem high, is actually quite low, especially considering all the possible things one might want to control when using this plugin. Bob has made the mistake of offering a lot of ridiculously specific options, rendering his plugin much more difficult to customize!</p>

<h4>A better model</h4>

<p>So it's pretty obvious: Bob needs a new customization model, one which does not relinquish control or abstract away the necessary details.</p>

<p>The reason Bob is so drawn to this high-level simplicity is that the jQuery framework very much lends itself to this mindset. Offering a <code>previousButtonTextColor</code> option is nice and simple, but let's face it, the vast majority of plugin users are going to want way more control!</p>

<p>Here are a few tips which should help you create a better set of customizable options for your plugins:</p>

<h4>Don't create plugin-specific syntax</h4>

<p>Developers who use your plugin shouldn't have to learn a new language or terminology just to get the job done.</p>

<p>Bob thought he was offering maximum customization with his <code>delay</code> option (look above). He made it so that with his plugin you can specify four different delays, "quite short," "very short," "quite long," or "very long":</p>

<div class="igBar"><span id="ljavascript-10"><a href="#" onclick="javascript:showPlainTxt('javascript-10'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-10">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> delayDuration <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">0</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">delay</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'very short'</span> <span style="color: #339933;">:</span> delayDuration <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">100</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'quite short'</span> <span style="color: #339933;">:</span> delayDuration <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">200</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'quite long'</span> <span style="color: #339933;">:</span> delayDuration <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'very long'</span> <span style="color: #339933;">:</span> delayDuration <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">400</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">default</span> <span style="color: #339933;">:</span> delayDuration <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">200</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>Not only does this limit the level of control people have, but it takes up quite a bit of space. Twelve lines of code just to define the delaying time is a bit much, don't you think? A better way to construct this option would be to let plugin users specify the amount of time (in milliseconds) as a number, so that no processing of the option needs to take place.</p>

<p>The key here is not to diminish the level of control through your abstraction. Your abstraction, whatever it is, can be as simplistic as you want, but make sure that people who use your plugin will still have that much-sought-after low-level control! (By low-level I mean non-abstracted)</p>

<h4>Give full control of elements</h4>

<p>If your plugin creates elements to be used within the DOM, then it's a good idea to offer plugin users some way to access those elements. Sometimes this means giving certain elements IDs or classes. But note that your plugin shouldn't rely on these hooks internally:</p>

<p>A bad implementation:</p>

<div class="igBar"><span id="ljavascript-11"><a href="#" onclick="javascript:showPlainTxt('javascript-11'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-11">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Plugin code</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;the_gallery_Wrapper&quot; /&gt;'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#the_gallery_wrapper'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'...'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>A good implementation:</p>

<div class="igBar"><span id="ljavascript-12"><a href="#" onclick="javascript:showPlainTxt('javascript-12'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-12">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Retain an internal reference:</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $wrapper <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div /&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">wrapperAttrs</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">container</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$wrapper.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'...'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// Easy to reference later...</span></div></li>
</ol></div>
</div></div><br />

<p>Notice that we've created a reference to the injected wrapper and we're also calling the 'attr' method to add any specified attributes to the element. So, in our settings it might be handled like this:</p>

<div class="igBar"><span id="ljavascript-13"><a href="#" onclick="javascript:showPlainTxt('javascript-13'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-13">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; wrapperAttrs <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; id <span style="color: #339933;">:</span> <span style="color: #3366CC;">'gallery-wrapper'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ... rest of settings ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// We can use the extend method to merge options/settings as usual:</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// But with the added first parameter of TRUE to signify a DEEP COPY:</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> settings <span style="color: #339933;">=</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The <code>$.extend()</code> method will now recurse through all nested objects to give us a merged version of both the defaults and the passed options, giving the passed options precedence.</p>

<p>The plugin user now has the power to specify any attribute of that wrapper element &mdash; so if they require that there be a hook for any CSS styles then they can quite easily add a class or change the name of the ID without having to go digging around in plugin source.</p>

<p>The same model can be used to let the user define CSS styles:</p>

<div class="igBar"><span id="ljavascript-14"><a href="#" onclick="javascript:showPlainTxt('javascript-14'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-14">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; wrapperCSS <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ... rest of settings ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Later on in the plugin where we define the wrapper:</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $wrapper <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div /&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">wrapperAttrs</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">wrapperCSS</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// ** Set CSS!</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span>settings.<span style="color: #660066;">container</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Your plugin may have an associated StyleSheet where developers can add CSS styles. Even in this situation it's a good idea to offer some convenient way of setting styles in JavaScript, without having to use a selector to get at the elements.</p>

<h4>Provide callback capabilities</h4>

<p><strong>What is a callback?</strong> - A callback is essentially a function to be called later, normally triggered by an event. It's passed as an argument, usually to the initiating call of a component. (in this case, a jQuery plugin).</p>

<p>If your plugin is driven by events then it might be a good idea to provide a callback capability for each event. Plus, you can create your own custom events and then provide callbacks for those. In this gallery plugin it might make sense to add an 'onImageShow' callback.</p>

<div class="igBar"><span id="ljavascript-15"><a href="#" onclick="javascript:showPlainTxt('javascript-15'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-15">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; onImageShow <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// we define an empty anonymous function</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// so that we don't need to check its</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// existence before calling it.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ... rest of settings ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Later on in the plugin:</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$nextButton.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> showNextImage<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> showNextImage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// DO stuff to show the image here...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Here's the callback:</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; settings.<span style="color: #660066;">onImageShow</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>Instead of initiating the callback via traditional means (adding parenthesis) we're calling it in the context of 'this' which will be a reference to the image node. This means that you have access to the actual image node through the 'this' keyword within the callback:</p>

<div class="igBar"><span id="ljavascript-16"><a href="#" onclick="javascript:showPlainTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-16">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul.imgs li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">superGallery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; onImageShow <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span&gt;'</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'longdesc'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ... other options ...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Similarily you could add an "onImageHide" callback and numerous other ones...</p>

<p>The point with callbacks is to give plugin users an easy way to add additional functionality without digging around in the source.</p>

<h4>Remember, it's a compromise</h4>

<p>Your plugin is not going to be able to work in every situation. And equally, it's not going to be very useful if you offer no or very few methods of control. So, remember, it's always going to be a compromise. Three things you must always take into account are:</p>

<ul>
    <li><strong>Flexibility</strong>: How many situations will your plugin be able to deal with?</li>
    <li><strong>Size</strong>: Does the size of your plugin correspond to its level of functionality? I.e. Would you use a very basic tooltip plugin if it was 20k in size? - Probably not!</li>
    <li><strong>Performance</strong>: Does your plugin heavily process the options in any way? Does this effect speed? Is the overhead caused worth it for the end user?</li>
</ul>
[inline] [/inline]]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/03/making-a-jquery-plugin-truly-customizable/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Implementing Prototype&#8217;s Array Methods in jQuery</title>
		<link>http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery</link>
		<comments>http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery#comments</comments>
		<pubDate>Sat, 21 Feb 2009 02:20:32 +0000</pubDate>
		<dc:creator>Josh Powell</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=450</guid>
		<description><![CDATA[One of the biggest concerns I've heard mentioned from users of the Prototype library about jQuery is the lack of support for various array methods.  The robust features Prototype provides for arrays is of great benefit to developers that do a lot of array manipulation in their JavaScript.  

However, I find that after [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest concerns I've heard mentioned from users of the Prototype library about jQuery is the lack of support for various array methods.  The robust features Prototype provides for arrays is of great benefit to developers that do a lot of array manipulation in their JavaScript.  </p>

<p>However, I find that after moving to jQuery, I do less array manipulation than I had done with Prototype. Perhaps jQuery has altered my development pattern so I no longer need array manipulation, or perhaps I have shifted most of my data manipulation to the server. Whatever the case, I have only on occasion missed the Prototype array methods.</p>
 <span id="more-450"></span>
<p>While there is some overlap in the ways that jQuery and Prototype handle array manipulation, jQuery does a few things Prototype doesn't do, and Prototype does a number of things that jQuery doesn't. I began writing an article about these differences, but soon got side-tracked writing a jQuery plugin to mimic the array methods Prototype provides.</p>
[inline][/inline]
<p>You can find the plugin at <a href="http://code.google.com/p/jquery-protify-js/">http://code.google.com/p/jquery-protify-js/</a></p>
 
<p>With this plugin, you can give a particular array all of the methods that Prototype adds to their <code>Array</code> and <code>Enumerable</code> objects. You can use the methods two different ways. The first does not extend the original array:</p>
 
<div class="igBar"><span id="ljavascript-28"><a href="#" onclick="javascript:showPlainTxt('javascript-28'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-28">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> protArray <span style="color: #339933;">=</span> $.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This will return an array extended with the Prototype library's methods, but leave the original array untouched.</p> 

<p>The second extends the original array by passing in <code>true</code> as the second parameter:</p>
 
<div class="igBar"><span id="ljavascript-29"><a href="#" onclick="javascript:showPlainTxt('javascript-29'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-29">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>In either case, the JavaScript Array prototype is untouched. When you create new arrays, it will not have the new methods. This way of writing code adds the new methods when they are needed while leaving the underlying JavaScript prototypes untouched. The return value of the methods is an array with the extended methods so that they can be chained.</p>
 
<div class="igBar"><span id="ljavascript-30"><a href="#" onclick="javascript:showPlainTxt('javascript-30'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-30">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr2 <span style="color: #339933;">=</span> $.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">&gt;=</span><span style="color: #CC0000;color:#800000;">3</span>; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">first</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// 3</span></div></li>
</ol></div>
</div></div><br />
              
<p>The above code first returns the <code>arr</code> array values in a new array with the Prototype methods, removes any <code>null</code>/<code>undefined</code> values (using <code>compact</code>), finds all the values in the array greater than or equal to 3 and returns an extended array of them (using <code>findAll</code>), and finally returns the first value of that array (using <code>first</code>).  It does all of this without touching the prototype for all arrays.</p>
 
<p>Here are some useful Prototype array methods included in the plugin:</p>

<h4>all()</h4>
<p>returns <code>true</code> if every member of the array <code>== true</code> and returns <code>false</code> if even one member <code>!= true</code>.</p>

<div class="igBar"><span id="ljavascript-31"><a href="#" onclick="javascript:showPlainTxt('javascript-31'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-31">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// true</span></div></li>
</ol></div>
</div></div><br />

<h4>any()</h4>
<p>returns true if even one member of the array <code>== true</code> and returns false if every member <code>!= true</code>.</p>

<div class="igBar"><span id="ljavascript-32"><a href="#" onclick="javascript:showPlainTxt('javascript-32'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-32">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> $.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">any</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// true</span></div></li>
</ol></div>
</div></div><br />

<h4>map()</h4> 
<p>similiar to jQuery's <code>$.map</code>.  It executes the function passed in on every of member of the array and returns an array of the results.</p>

<div class="igBar"><span id="ljavascript-33"><a href="#" onclick="javascript:showPlainTxt('javascript-33'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-33">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=map"><span style="">map</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> n <span style="color: #339933;">*</span> <span style="color: #CC0000;color:#800000;">2</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// &nbsp;[2,4,6,8,10,12]</span></div></li>
</ol></div>
</div></div><br />

<h4>eachSlice(number)</h4> 
<p>slices the array into an array of arrays of the size passed in.  There is an optional second parameter which is a function that acts on each array partition before placing it in the slice.</p>

<div class="igBar"><span id="ljavascript-34"><a href="#" onclick="javascript:showPlainTxt('javascript-34'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-34">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">eachSlice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// [[1,2],[3,4],[5,6]]</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">eachSlice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> n.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=map"><span style="">map</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;color:#800000;">2</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// [[2,4],[6,8],[10,12]]</span></div></li>
</ol></div>
</div></div><br />

<h4>include(value)</h4>
<p>returns <code>true</code> if the value is in the array (using == equality).</p>

<div class="igBar"><span id="ljavascript-35"><a href="#" onclick="javascript:showPlainTxt('javascript-35'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-35">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// true</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pie'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// false</span></div></li>
</ol></div>
</div></div><br />

<h4>max()/min()</h4> 

<p>returns the maximum/minimum value of the array.  Optional parameter is a function that can be used to define the comparison and what gets returned.</p>

<div class="igBar"><span id="ljavascript-36"><a href="#" onclick="javascript:showPlainTxt('javascript-36'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-36">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'frank'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'joe'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> person.<span style="color: #660066;">age</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// returns 12</span></div></li>
</ol></div>
</div></div><br />

<h4>partition()</h4>
<p>returns an array with two arrays in it.  The first array contains the values that <code>== true</code> in the initial array, and the second all of the values <code>!= true</code>. There is an optional parameter that is a function used to do the evaluation.</p>

<div class="igBar"><span id="ljavascript-37"><a href="#" onclick="javascript:showPlainTxt('javascript-37'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-37">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">partition</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> n <span style="">&lt;</span><span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">3</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// [[1,2,3],[4,5,6]]</span></div></li>
</ol></div>
</div></div><br />
<br />
<h4>pluck()</h4>

<p>And finally, my favorite.  The <code>pluck()</code> method iterates through every member of the array and returns an array of the value of the attribute name passed in.</p>

<div class="igBar"><span id="ljavascript-38"><a href="#" onclick="javascript:showPlainTxt('javascript-38'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-38">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Frank'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Joe'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluck</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'age'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// [10, 12]</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluck</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// ['Frank, 'Joe']</span></div></li>
</ol></div>
</div></div><br />

<p>For a full listing and description of all the methods, see the Prototype documentation on arrays and enumerables.</p>

<p>Arrays: <a href="http://prototypejs.org/api/array">http://prototypejs.org/api/array</a></p>

<p>Enumerables: <a href="http://prototypejs.org/api/enumerable">http://prototypejs.org/api/enumerable</a></p>

<p>The <code>toJSON</code> method has not yet been implemented because it depends on object methods that Prototype provides.  Can you guess what my next project is?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Peeling Away the jQuery Wrapper and Finding an Array</title>
		<link>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper</link>
		<comments>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper#comments</comments>
		<pubDate>Mon, 22 Dec 2008 16:46:56 +0000</pubDate>
		<dc:creator>Cody Lindley</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[objects]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=196</guid>
		<description><![CDATA[If you haven't poked around under the hood of jQuery, you might not be aware that when you pass the jQuery function an expression or DOM element it places these elements (or, possibly a single element) into an object, and then this object is returned so that it can be chained. Without getting into the [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven't poked around under the hood of jQuery, you might not be aware that when you pass the jQuery function an expression or DOM element it places these elements (or, possibly a single element) into an object, and then this object is returned so that it can be chained. Without getting into the details of chaining, the fundamental concept to remember is this:</p>

<p><em>Most jQuery methods return the jQuery object itself, which allows methods to be chained.</em></p>
<span id="more-196"></span>
<p>The interesting thing about the jQuery object is that while its datatype is an object, it has array-like characteristics:</p>
<ul>
	<li>its property names (the ones that refer to DOM elements, at least) are numeric</li>
	<li>it has a length property</li>
</ul>
<p>Have you ever noticed, for example, a snippet of jQuery code that directly accesses a DOM element by using the bracket operator with the jQuery function?</p>

<div class="igBar"><span id="ljavascript-46"><a href="#" onclick="javascript:showPlainTxt('javascript-46'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-46">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>; <span style="color: #006600; font-style: italic;">// give me the first DOM element that the</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// jQuery function found</span></div></li>
</ol></div>
</div></div><br />

<p>Now, the jQuery function returns an object, but it looks like we just treated it as an array. It can also be written like this:</p>

<div class="igBar"><span id="ljavascript-47"><a href="#" onclick="javascript:showPlainTxt('javascript-47'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-47">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// do the same, using a jQuery method</span></div></li>
</ol></div>
</div></div><br />

<p>If this is confusing, let's see if we can clear it up with some code. Go to the jQuery.com homepage, open up <a href="http://getfirebug.com/">Firebug</a> using Firefox, and run the following line of code in the console.</p>

<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showPlainTxt('javascript-48'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-48">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// toSource() is a method of all JavaScript objects</span></div></li>
</ol></div>
</div></div><br />

<p>You should be looking at this in the console:</p>

<code>"({length:25, 0:{}, 1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}, 7:{}, 8:{}, 9:{}, 10:{}, 11:{}, 12:{}, 13:{}, 14:{}, 15:{}, 16:{}, 17:{}, 18:{}, 19:{}, 20:{}, 21:{}, 22:{}, 23:{}, 24:{}, prevObject:{0:{}, length:1}})"</code>

<p>Looky there! The jQuery function is returning an object with a bunch of properties. If we tack on the bracket operator to the jQuery function, which returns an object, we can access the properties of that object because inside the object are properties with a numeric property name. Fascinating isn't it?</p>

<p>I know what you're thinking: this tutorial is about arrays, not objects. Right, so let's get to the array part. Since we now know that the jQuery function returns an object that only masquerades as a JavaScript array the question at hand is how do I get a true JavaScript array from a jQuery wrapper object. This is actually very simple. Most of you might not have been aware (if not, I showed you earlier) that <code>$('div')[0]</code> and <code>$('div').get(0)</code> return a reference to the first element in the jQuery wrapper object. But, the <code>get()</code> method actually has another purpose. It can quickly turn the DOM elements found in the jQuery wrapper into an array of DOM elements.</p>

<p>Let's return to jQuery.com and open up Firebug again. Inside the console run the following line of code.</p>

<div class="igBar"><span id="ljavascript-49"><a href="#" onclick="javascript:showPlainTxt('javascript-49'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-49">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>You should now be looking at this in the firebug console:</p>

<p><code>"[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]"</code></p>

<p>Essentially the <code>.get()</code> method placed all of the DOM elements inside the jQuery wrapper into an array and returned that array. So now you can use default array methods on your array of elements. For example:</p>

<div class="igBar"><span id="ljavascript-50"><a href="#" onclick="javascript:showPlainTxt('javascript-50'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-50">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span>; <span style="color: #006600; font-style: italic;">//returns the array as a string</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//this is a method of the array object</span></div></li>
</ol></div>
</div></div><br />

<p>Additionally, since we now have an array we can write a statement like this:</p>

<div class="igBar"><span id="ljavascript-51"><a href="#" onclick="javascript:showPlainTxt('javascript-51'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-51">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>; <span style="color: #006600; font-style: italic;">//returns the first div's id attribute value</span></div></li>
</ol></div>
</div></div><br />

<p>There is something worth noting here that might not be obvious to those starting out with jQuery or JavaScript. Since we are now returning an array, the chain is broken. You can't chain any jQuery methods after using the <code>.get()</code> method.</p>

<p>But, heck, you have an array. Now you can use the utility functions that jQuery provides (<code>$.each()</code>, <code>$.grep()</code>, <code>$.map()</code>, <code>$.inArray()</code>, <code>$.unique()</code>), as well as core JavaScript methods and properties, to work on the array. I'm not going to demonstrate all of the jQuery utility functions that can be used on an array, but I do want to examine one of them here in this tutorial.</p>

<p>The <code>$.each()</code> utility function, not to be confused with the <code>$('selector').each()</code> method used to operate on a set of DOM elements in a jQuery wrapper, can be used to seamlessly iterate over an array or object. For example, let's say that you would like to store (or cache) a set of <code>&lt;li&gt;</code> elements in an array, because maybe you are going to remove them from the DOM temporarily but eventually will be placing them back on the page. To do this you could use the <code>$.each()</code> utility function to loop over an array and append each <code>&lt;li&gt;</code> element back to the page. In the code example below this is done on page load by converting and storing the jQuery wrapper of <code>&lt;li&gt;</code> elements to an array, and then looping over this array in order to place them back on the page.</p>

<div class="igBar"><span id="ljavascript-52"><a href="#" onclick="javascript:showPlainTxt('javascript-52'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-52">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;html&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;head&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;script</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;/head&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;body&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;ul&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">6</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">7</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>task <span style="color: #CC0000;color:#800000;">8</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;/ul&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;script&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arrayList <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul li'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=empty"><span style="">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span>arrayList<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li&gt;&lt;del&gt;'</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/del&gt; complete&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;/script&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;/body&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;/html&gt;</span></div></li>
</ol></div>
</div></div><br />

<p>Notice that I am forgoing the use of the <code>.ready()</code> event by simply placing my jQuery code before the closing body element.</p>

<p>So, hopefully it's obvious how extremely handy it can be at times to make an array out of a jQuery wrapper. In fact, once you are dealing with an array, you can use a handy plugin called <a href="http://plugins.jquery.com/project/rich-array">Rich Array</a>. This plugin provides the following additional utility functions for arrays. And yes, there is a bit of duplication here with some of the core utility functions.</p>

<p>
<strong>$.richArray.in()</strong> //Checks whether an array contains some value<br />
<strong>$.richArray.unique()</strong> //Produces the duplicate-free version of the array<br />
<strong>$.richArray.diff()</strong> //Finds the difference between two arrays.<br />
<strong>$.richArray.intersect()</strong> //Finds the intersection of two arrays<br />
<strong>$.richArray.filter()</strong> //Applies filter to the array, using callback function<br />
<strong>$.richArray.map()</strong> //Applies callback function for each element in the input array, and returns array of values that this function returned<br />
<strong>$.richArray.sum()</strong> //Computes the sum of all array elements<br />
<strong>$.richArray.product()</strong> //Calculates the production of all elements of the array<br />
<strong>$.richArray.reduce()</strong> //Reduces the array. One-element arrays are turned into their unique element<br />
<strong>$.richArray.compact()</strong> //Creates new version of array without null/undefined values<br />
<strong>$.richArray.without()</strong> //Creates a new version of the array that doesn't contain the specified value</p>

[inline][/inline]]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper/feed</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Working with Events, part 2</title>
		<link>http://www.learningjquery.com/2008/05/working-with-events-part-2</link>
		<comments>http://www.learningjquery.com/2008/05/working-with-events-part-2#comments</comments>
		<pubDate>Mon, 19 May 2008 12:47:05 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[namespacing]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2008/05/working-with-events-part-2</guid>
		<description><![CDATA[In my last article, I described the common problem of events seemingly ceasing to work for new elements added to a document, whether by some form of ajax or by DOM modification. We examined one way to overcome the problem: Event Delegation. With event delegation, we bind the event handler to a containing element that [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.learningjquery.com/2008/03/working-with-events-part-1">my last article</a>, I described the common problem of events seemingly ceasing to work for new elements added to a document, whether by some form of ajax or by DOM modification. We examined one way to overcome the problem: <strong>Event Delegation</strong>. With event delegation, we bind the event handler to a containing element that remains in the DOM and then check for the target of the event. 
</p>
<h4>Cloning Nodes</h4>

<p>This time, we'll take a look at re-binding event handlers. But before we do, I should mention that, as of jQuery version 1.2, event handlers can be cloned along with elements. Consider this unordered list:
</p>
<span id="more-94"></span>
[inline][/inline]
<div class="igBar"><span id="lhtml-63"><a href="#" onclick="javascript:showPlainTxt('html-63'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-63">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;list3&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;eventlist&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>plain<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special&quot;</span><span style="color: #000000;">&gt;</span></span>special <span style="color: #009900;"><span style="color: #000000;">&lt;button&gt;</span></span>I am special<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>button&gt;&lt;<span style="color: #66cc66;">/</span>li&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>plain<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>We can make a copy of <code>&lt;li class="special"&gt;</code> and insert it after the original, while at the same time retaining any event handlers that were attached within the original. The plain <code>.clone()</code> method doesn't work that way; instead, it just copies the element:</p>

<div class="igBar"><span id="ljavascript-64"><a href="#" onclick="javascript:showPlainTxt('javascript-64'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-64">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list3 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $parent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $parent.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=clone"><span style="">clone</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span>$parent<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

Try it:

<ul id="list3" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<p>As you can see, the original button is able to keep creating new list items, but the buttons inside the "dynamically generated" list items can't create new ones.</p>

<p>To get the event handlers copied over as well, all we have to do is pass in <code>true</code> to the method's single argument:</p>

<div class="igBar"><span id="ljavascript-65"><a href="#" onclick="javascript:showPlainTxt('javascript-65'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-65">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list4 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $parent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $parent.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=clone"><span style="">clone</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' I<span style="color: #000099; font-weight: bold;">\'</span>m a clone!'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span>$parent<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Note: I've added <code>.append(' I\'m a clone!')</code> here only as visual reinforcement of what is going on.</p>

<p>Try it now:</p>

<ul id="list4" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<p>Using <code>.clone(true)</code> is great when we want to make a copy of existing elements and their event handlers, but there are plenty of other situations that don't involve cloning in which we might want event handlers to persist.</p>
<h4>Re-binding Basics</h4>
<p>The basic concept behind re-binding is fairly straightforward: We create a function that binds the handlers and then call it whenever new elements are introduced. For example, with our unordered list above, we first create a function called <code>addItem</code> that registers the click handler, which in turn will add a new item:</p>

<div class="igBar"><span id="ljavascript-66"><a href="#" onclick="javascript:showPlainTxt('javascript-66'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-66">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> addItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list5 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>Next, we call that function when the DOM initially loads:</p>

<div class="igBar"><span id="ljavascript-67"><a href="#" onclick="javascript:showPlainTxt('javascript-67'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-67">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; addItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Finally, we can call the function inside the click handler&mdash;and inside itself. That way, it will bind the event handlers to the new list item as well. </p>

<p>We'll add one more click handler to the button, but this one will not be re-bound, so that we can see the difference.</p>

<p>Here is what the code for buttons in #list5 looks like, all together:</p>
<div class="igBar"><span id="ljavascript-68"><a href="#" onclick="javascript:showPlainTxt('javascript-68'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-68">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> addItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list5 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; addItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; addItem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// non-rebinding click handler ...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list5 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' pressed'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Try this one out:</p>

<ul id="list5" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>
<p>We can see that "pressed" is appended to the first list item each time it is clicked, but it is not appended to any of the list items created by our clicks. On the other hand, the created buttons are able to generate further list items because that function is being rebound.</p>

<p>However, what we've just done produces unwelcome results if we click on a button more than once. The click handler is bound again with each click of a button, producing a multiplier effect. The first click of a button creates one extra list item; the second creates two; the third, four; and so on.</p>

<h4>Unbind, then Bind</h4>
<p>To avoid the multiple binding, we can unbind first and then re-bind. So in line 2, instead of this ...</p>
<p><code>$('#list5 li.special button').click(function() {</code></p>
  
<p>... we'll have this ...</p>
<p><code>$('#list6 li.special button').unbind('click').bind('click', function() {</code></p>

<p>Note the use of <code>.bind()</code> here. This is the universal event binder that jQuery uses. All the others, such as <code>.click()</code>, <code>.blur()</code>, <code>.resize()</code>, and so on, are shorthand methods for their <code>.bind('event')</code> equivalent. </p>

<p>The complete new code, again with the additional non-rebinding click handler for contrast, looks like this:</p>

<div class="igBar"><span id="ljavascript-69"><a href="#" onclick="javascript:showPlainTxt('javascript-69'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-69">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> addItemUnbind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list6 li.special button'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=unbind"><span style="">unbind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; addItemUnbind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; addItemUnbind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// non-rebinding click handler</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list6 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' pressed'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>See how this one works:</p>

<ul id="list6" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<p>Unfortunately, our attempt to unbind the <code>addItemUnbind()</code> function went too far, unbinding the "non-rebinding" click handler as well, before it even had a chance to run once (it's evident because there is no "pressed" text after the "I am special" button here). Clearly, we're going to have to be more careful about what we're unbinding.</p>

<h4 id="event-namespacing">Event Namespacing</h4>
<p>One way to avoid the over-reaching event unbinding is to apply a "namespace" to the click event for both binding and unbinding. So, instead of <code>.bind('click')</code> and <code>.unbind('click)</code>, we'll have, for example, <code>.bind('click.addit')</code> and <code>.unbind('click.addit)</code>. Here is one last code sample, which looks identical to the previous, except that it now has the namespaced event (and the list id is list7):</p>

<div class="igBar"><span id="ljavascript-70"><a href="#" onclick="javascript:showPlainTxt('javascript-70'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-70">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> addItemNS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list7 li.special button'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=unbind"><span style="">unbind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.addit'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.addit'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; addItemNS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; addItemNS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// non-rebinding click handler</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list7 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' pressed'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>We should now &mdash; finally! &mdash; have a set of behaviors attached to these buttons that act the way we intend them to:</p>

<ul id="list7" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<p>For more information about event namespacing, read Brandon Aaron's article, <a href="http://www.learningjquery.com/2007/09/namespace-your-events">Namespace Your Events</a>.</p>
<h4>Bonus: Unbind by Function Reference</h4>
<p>If you've made it this far, then you must have extraordinary patience, in which case I'll reward it with one final method of rebinding. Rather than namespace the events, we can reference the function in the second argument of the <code>.bind()</code> and <code>.unbind()</code> methods. We have to shuffle things around a bit to avoid "too much recursion," but it'll do just fine like so:</p>

<div class="igBar"><span id="ljavascript-71"><a href="#" onclick="javascript:showPlainTxt('javascript-71'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-71">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> addItemFinal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list8 li.special button'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=unbind"><span style="">unbind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> addItemFinal<span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> addItemFinal<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list8 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> addItemFinal<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// non-rebinding click handler</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list8 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' pressed'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Note here that there are no parentheses after "addItemFinal" when it appears inside the bind/unbind, because we are <em>referencing</em> the function, not calling it. So let's test it out one last time:</p>

<ul id="list8" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<h4>Plugin Options</h4>
<p>There are three great plugins that can do a lot of this work for us:</p> 

<ul>
  <li><a href="http://plugins.jquery.com/project/livequery">Live Query</a> by <a href="http://blog.brandonaaron.net/">Brandon Aaron</a></li>
  <li><a href="http://plugins.jquery.com/project/Listen">Listen</a> by <a href="http://flesler.blogspot.com/">Ariel Flesler</a></li>
  <li><a href="http://plugins.jquery.com/project/Intercept">Intercept</a> by Ariel Flesler</li>
</ul>

<p>If this entry left you bewildered, or if you want a quick, well tested solution, you should definitely try one of them. Each works a little differently, but they're all super plugins.</p>

<h4 id="add-remove"><ins datetime="2008-12-07T21:40:30+00:00">Update</ins>: Event Namespacing with Add and Remove</h4>
<p>In response to the comment by <a href="#comment-63755">Nick Johns below</a>, I'm providing an example that allows for both adding a row and removing a row. The code is based on the "<a href="#event-namespacing">Event Namespacing</a>" example above:</p>

<div class="igBar"><span id="ljavascript-72"><a href="#" onclick="javascript:showPlainTxt('javascript-72'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-72">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">function</span> addRemoveItemNS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button class=&quot;addone&quot;&gt;I am new&lt;/button&gt; &lt;button class=&quot;removeme&quot;&gt;remove me&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list9 li.special'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.addone'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=unbind"><span style="">unbind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.addit'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.addit'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; addRemoveItemNS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=end"><span style="">end</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.removeme'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=unbind"><span style="">unbind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.removeit'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click.removeit'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=remove"><span style="">remove</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; addRemoveItemNS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>I added an "addone" class to the initial button, but otherwise the list is the same as the others. You can try it out here:</p>

<ul id="list9" class="eventlist">
<li>plain</li>
<li class="special">special <button class="addone">I am special</button></li>
<li>plain</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/05/working-with-events-part-2/feed</wfw:commentRss>
		<slash:comments>51</slash:comments>
		</item>
		<item>
		<title>A Plugin Development Pattern</title>
		<link>http://www.learningjquery.com/2007/10/a-plugin-development-pattern</link>
		<comments>http://www.learningjquery.com/2007/10/a-plugin-development-pattern#comments</comments>
		<pubDate>Tue, 30 Oct 2007 17:44:13 +0000</pubDate>
		<dc:creator>Mike Alsup</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2007/10/a-plugin-development-pattern</guid>
		<description><![CDATA[I've been developing jQuery plugins for quite a while now, and I've become rather comfortable with a particular style of plugin development for my scripts. This article is meant to share the pattern that I've found especially useful for plugin authoring. It assumes you already have an understanding of plugin development for jQuery; if you're [...]]]></description>
			<content:encoded><![CDATA[<p>I've been developing jQuery plugins for quite a while now, and I've become rather comfortable with a particular style of plugin development for my scripts. This article is meant to share the pattern that I've found especially useful for plugin authoring. It assumes you already have an understanding of plugin development for jQuery; if you're a novice plugin author, please review the <a href="http://docs.jquery.com/Plugins/Authoring">jQuery Authoring Guidelines</a> first.</p>

<p>There are a few <em>requirements</em> that I feel this pattern handles nicely:</p>
<ol>
	<li>Claim only a single name in the jQuery namespace</li>
	<li>Accept an options argument to control plugin behavior</li>
	<li>Provide public access to default plugin settings</li>
	<li>Provide public access to secondary functions (as applicable)</li>
	<li>Keep private functions private</li>
	<li>Support the Metadata Plugin</li>
</ol>
<p>I'll cover these requirements one by one, and as we work through them we'll build a simple plugin which highlights text.</p>
<span id="more-83"></span>
<h4>Claim only a single name in the jQuery namespace</h4>
<p>This implies a <em>single</em>-plugin script. If your script contains multiple plugins, or complementary plugins (like $.fn.doSomething() and $.fn.undoSomething()) then you'll claim multiple names are required.  But in general when authoring a plugin, strive to use only a single name to hold all of its implementation details.</p>

<p>In our example plugin we will claim the name "hilight".</p>
<div class="igBar"><span id="ljavascript-87"><a href="#" onclick="javascript:showPlainTxt('javascript-87'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-87">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// Our plugin implementation code goes here.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />

<p>And our plugin can be invoked like this:</p>
<div class="igBar"><span id="ljavascript-88"><a href="#" onclick="javascript:showPlainTxt('javascript-88'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-88">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hilight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>But what if we need to break up our implementation into more than one function? There are many reasons to do so:  the design may require it;  it may result in a simpler or more readable implementation; and it may yield better <abbr title="Object Oriented">OO</abbr> semantics.</p>

<p>It's really quite trivial to break up the implementation into multiple functions without adding noise to the namespace. We do this by recognizing, and taking advantage of, the fact that <em>functions are first-class objects</em> in JavaScript. Like any other object, functions can be assigned properties.  Since we have already claimed the "hilight" name in the jQuery prototype object, any other properties or functions that we need to expose can be declared as properties on our "hilight" function.  More on this later.</p>
<h4>Accept an options argument to control plugin behavior</h4>
<p>Let's add support to our hilight plugin for specifying the foreground and background colors to use.  We should allow options like these to be passed as an options object to the plugin function.  For example:</p>

<div class="igBar"><span id="ljavascript-89"><a href="#" onclick="javascript:showPlainTxt('javascript-89'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-89">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; foreground<span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; background<span style="color: #339933;">:</span> <span style="color: #3366CC;">'yellow'</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// Extend our default options with those provided.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// Our plugin implementation code goes here.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Now our plugin can be invoked like this:</p>
<div class="igBar"><span id="ljavascript-90"><a href="#" onclick="javascript:showPlainTxt('javascript-90'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-90">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hilight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; foreground<span style="color: #339933;">:</span> <span style="color: #3366CC;">'blue'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>Provide public access to default plugin settings</h4>
<p>An improvement we can, and should, make to the code above is to expose the default plugin settings. This is important because it makes it very easy for plugin users to override/customize the plugin with minimal code. And this is where we begin to take advantage of the function object.</p>

<div class="igBar"><span id="ljavascript-91"><a href="#" onclick="javascript:showPlainTxt('javascript-91'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-91">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// Extend our default options with those provided.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// Note that the first arg to extend is an empty object -</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// this is to keep from overriding our &quot;defaults&quot; object.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// Our plugin implementation code goes here.</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// plugin defaults - added as a property on our plugin function</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; foreground<span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; background<span style="color: #339933;">:</span> <span style="color: #3366CC;">'yellow'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Now users can include a line like this in their scripts:</p>

<div class="igBar"><span id="ljavascript-92"><a href="#" onclick="javascript:showPlainTxt('javascript-92'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-92">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// this need only be called once and does not</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// have to be called from within a 'ready' block</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span>.<span style="color: #660066;">foreground</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'blue'</span>;</div></li>
</ol></div>
</div></div><br />

<p>And now we can call the plugin method like this and it will use a blue foreground color:</p>

<div class="igBar"><span id="ljavascript-93"><a href="#" onclick="javascript:showPlainTxt('javascript-93'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-93">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hilight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>As you can see, we've allowed the user to write a single line of code to alter the default foreground color of the plugin.  And users can still selectively override this new default value when they want:</p>

<div class="igBar"><span id="ljavascript-94"><a href="#" onclick="javascript:showPlainTxt('javascript-94'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-94">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// override plugin default foreground color</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span>.<span style="color: #660066;">foreground</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'blue'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// invoke plugin using new defaults</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.hilightDiv'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hilight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// override default by passing options to plugin method</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#green'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hilight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; foreground<span style="color: #339933;">:</span> <span style="color: #3366CC;">'green'</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>Provide public access to secondary functions as applicable</h4>
<p>This item goes hand-in-hand with the previous item and is an interesting way to extend your plugin (and to let others extend your plugin). For example, the implementation of our plugin may define a function called "format" which formats the hilight text.  Our plugin may now look like this, with the default implementation of the format method defined below the hilight function.</p>

<div class="igBar"><span id="ljavascript-95"><a href="#" onclick="javascript:showPlainTxt('javascript-95'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-95">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// iterate and reformat each matched element</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> markup <span style="color: #339933;">=</span> $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// call our format function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; markup <span style="color: #339933;">=</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">format</span><span style="color: #009900;">&#40;</span>markup<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span>markup<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// define our format function</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">format</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #3366CC;"> &nbsp;return '</span><span style="">&lt;strong&gt;</span><span style="color: #3366CC;">' + txt + '</span><span style="">&lt;/strong&gt;</span><span style="color: #3366CC;">';</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #3366CC;">};</span></div></li>
</ol></div>
</div></div><br />

<p>We could have just as easily supported another property on the options object that allowed a callback function to be provided to override the default formatting.  That's another excellent way to support customization of your plugin.  The technique shown here takes this a step further by actually exposing the format function so that it can be redefined. With this technique it would be possible for others to ship their own custom overrides of your plugin נin other words, it means others can write plugins for your plugin.</p>

<p>Considering the trivial example plugin we're building in this article, you may be wondering when this would ever be useful.  One real-world example is the <a href="http://malsup.com/jquery/cycle/">Cycle Plugin</a>.  The Cycle Plugin is a slideshow plugin which supports a number of built-in transition effects נscroll, slide, fade, etc.  But realistically, there is no way to define every single type of effect that one might wish to apply to a slide transition.  And that's where this type of extensibility is useful.  The Cycle Plugin exposes a "transitions" object to which users can add their own custom transition definitions.  It's defined in the plugin like this:</p>

<div class="igBar"><span id="ljavascript-96"><a href="#" onclick="javascript:showPlainTxt('javascript-96'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-96">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">cycle</span>.<span style="color: #660066;">transitions</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;<span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This technique makes it possible for others to define and ship transition definitions that plug-in to the Cycle Plugin.</p>
<h4>Keep private functions private</h4>
<p>The technique of exposing part of your plugin to be overridden can be very powerful. But you need to think carefully about what parts of your implementation to expose. Once it's exposed, you need to keep in mind that any changes to the calling arguments or semantics may break backward compatibility. As a general rule, if you're not sure whether to expose a particular function, then you probably shouldn't.</p>

<p>So how then do we define more functions without cluttering the namespace and without exposing the implementation? This is a job for <em>closures</em>. To demonstrate, we'll add another function to our plugin called "debug". The debug function will log the number of selected elements to the <a href="http://getfirebug.com">Firebug</a> console. To create a closure, we wrap the entire plugin definition in a function (as detailed in the <a href="http://docs.jquery.com/Plugins/Authoring">jQuery Authoring Guidelines</a>).</p>

<div class="igBar"><span id="ljavascript-97"><a href="#" onclick="javascript:showPlainTxt('javascript-97'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-97">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// create closure</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; debug<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// private function for debugging</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> debug<span style="color: #009900;">&#40;</span>$obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #003366; font-weight: bold;">console</span> &#038;& window.<span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; window.<span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hilight selection count: '</span> <span style="color: #339933;">+</span> $obj.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=size"><span style="">size</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;<span style="color: #006600; font-style: italic;">// &nbsp;...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// end of closure</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Our "debug" method cannot be accessed from outside of the closure and thus is private to our implementation.
<h4>Support the Metadata Plugin</h4>
Depending on the type of plugin you're writing, adding support for the <a href="http://docs.jquery.com/Plugins/Metadata/metadata">Metadata Plugin</a> can make it even more powerful.  Personally, I love the Metadata Plugin because it lets you use unobtrusive markup to override plugin options (which is particularly useful when creating demos and examples). And supporting it is very simple!</p>
<p><strong>Update</strong>: This bit was optimized per suggestion in the comments.</p>

<div class="igBar"><span id="ljavascript-98"><a href="#" onclick="javascript:showPlainTxt('javascript-98'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-98">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// build main options before element iteration</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// build element specific options</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> $.<span style="color: #660066;">meta</span> <span style="color: #339933;">?</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> opts<span style="color: #339933;">,</span> $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> opts;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//...</span></div></li>
</ol></div>
</div></div><br />

<p>This changed line does a couple of things:</p>
<ul>
	<li>it tests to see if the Metadata Plugin is installed</li>
	<li>if it is installed, it extends our options object with the extracted metadata.</li>
</ul>
<p>This line is added as the last argument to <code>jQuery.extend</code> so it will override any other option settings. Now we can drive behavior from the markup if we choose:</p>
<pre><code>&lt;!--  markup  --&gt;
&lt;div class="hilight { background: 'red', foreground: 'white' }"&gt;
  Have a nice day!
&lt;/div&gt;
&lt;div class="hilight { foreground: 'orange' }"&gt;
  Have a nice day!
&lt;/div&gt;
&lt;div class="hilight { background: 'green' }"&gt;
  Have a nice day!
&lt;/div&gt;
</code></pre>
<p>And now we can hilight each of these divs uniquely using a single line of script:</p>
<div class="igBar"><span id="ljavascript-99"><a href="#" onclick="javascript:showPlainTxt('javascript-99'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-99">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.hilight'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hilight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>Putting it All Together</h4>
<p>Below is the completed code for our example:</p>

<div class="igBar"><span id="ljavascript-100"><a href="#" onclick="javascript:showPlainTxt('javascript-100'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-100">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// create closure</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// plugin definition</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; debug<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// build main options before element iteration</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> opts <span style="color: #339933;">=</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// iterate and reformat each matched element</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// build element specific options</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> $.<span style="color: #660066;">meta</span> <span style="color: #339933;">?</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.extend"><span style="">$.<span style="color: #660066;">extend</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> opts<span style="color: #339933;">,</span> $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> opts;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// update element styles</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; backgroundColor<span style="color: #339933;">:</span> o.<span style="color: #660066;">background</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; color<span style="color: #339933;">:</span> o.<span style="color: #660066;">foreground</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> markup <span style="color: #339933;">=</span> $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// call our format function</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; markup <span style="color: #339933;">=</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">format</span><span style="color: #009900;">&#40;</span>markup<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span>markup<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// private function for debugging</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> debug<span style="color: #009900;">&#40;</span>$obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #003366; font-weight: bold;">console</span> &amp;&amp; window.<span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; window.<span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hilight selection count: '</span> <span style="color: #339933;">+</span> $obj.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=size"><span style="">size</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// define and expose our format function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">format</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'&lt;strong&gt;'</span> <span style="color: #339933;">+</span> txt <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/strong&gt;'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// plugin defaults</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">hilight</span>.<span style="color: #660066;">defaults</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; foreground<span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; background<span style="color: #339933;">:</span> <span style="color: #3366CC;">'yellow'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// end of closure</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This design pattern has enabled me to create powerful, consistently crafted plugins. I hope it helps you to do the same.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2007/10/a-plugin-development-pattern/feed</wfw:commentRss>
		<slash:comments>163</slash:comments>
		</item>
		<item>
		<title>Selecting Elements by Properties and DOM Expandos</title>
		<link>http://www.learningjquery.com/2007/03/selecting-elements-by-properties-and-dom-expandos</link>
		<comments>http://www.learningjquery.com/2007/03/selecting-elements-by-properties-and-dom-expandos#comments</comments>
		<pubDate>Wed, 21 Mar 2007 12:22:40 +0000</pubDate>
		<dc:creator>Brandon Aaron</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2007/03/selecting-elements-by-properties-and-dom-expandos</guid>
		<description><![CDATA[Sometimes it is very convenient to store information on an element by using custom properties (DOM Expandos). For example, I use DOM Expandos in my Mouse Wheel plugin to store an array of handler methods (_mwHandlers) along with a method (_mwHandler) to call all those handlers when the mouse wheel is used. Storing these methods [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is very convenient to store information on an element by using custom properties (DOM Expandos). For example, I use DOM Expandos in my <a href="http://brandon.jquery.com/plugins/mousewheel/test/test.html">Mouse Wheel plugin</a> to store an array of handler methods (<code>_mwHandlers</code>) along with a method (<code>_mwHandler</code>) to call all those handlers when the mouse wheel is used. Storing these methods on the element allows me to write less code, and manage scope easier. It also gives me a function reference for easy removal when needed. </p>

<p>Now, what if I needed to select all elements that have a mouse wheel handler on them?</p>

<span id="more-56"></span>
<p>In jQuery 1.1 the attribute selector has an undocumented feature that will search DOM Expandos and properties as well as actual HTML attributes. Since I know that my handler method is named <code>_mwHandler</code>, I can use the following selector to find all elements in the document that have a mouse wheel event attached to them.</p>

<div class="igBar"><span id="ljavascript-104"><a href="#" onclick="javascript:showPlainTxt('javascript-104'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-104">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[@_mwHandler]'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>Search by Events</h4>
<p>Also, since the jQuery event system uses a DOM Expando to store events internally, you could find all elements that have an event attached to them using this selector:</p>

<div class="igBar"><span id="ljavascript-105"><a href="#" onclick="javascript:showPlainTxt('javascript-105'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-105">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[@events]'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><em>If you are using jQuery Revision 1371 or higher, the DOM Expando name has changed from 'events' to '$events'.</em></p>

<p>If you combine the previous selector with the filter method, you can find all elements that have a click event like this:</p>

<div class="igBar"><span id="ljavascript-106"><a href="#" onclick="javascript:showPlainTxt('javascript-106'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-106">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[@events]'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=filter"><span style="">filter</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=index"><span style="">index</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!!</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">events</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>Clean Up</h4>
<p>Most importantly, if you are using DOM Expandos, you should clean up after yourself to avoid memory leaks. Some instances of using DOM Expandos will not leak, but others will. So you're better off cleaning up, just in case. For example in the mouse wheel plugin I remove the DOM Expandos on a particular element inside the <code>unmousewheel</code> method. Then during the window unload event I loop through all the elements that still have a mouse wheel handler and call <code>unmousewheel</code> on them. In order to make this process as fast as possible I store a "cache" (just an Array) of elements that I have added DOM Expandos too. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2007/03/selecting-elements-by-properties-and-dom-expandos/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sacrificial Lambda</title>
		<link>http://www.learningjquery.com/2006/09/sacrificial-lambda</link>
		<comments>http://www.learningjquery.com/2006/09/sacrificial-lambda#comments</comments>
		<pubDate>Fri, 01 Sep 2006 13:36:28 +0000</pubDate>
		<dc:creator>Dan Bravender</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2006/09/sacrificial-lambda</guid>
		<description><![CDATA[Dynamically binding event handlers to content that has been ajaxed in with $.load without repeating yourself can be tricky. Lambda functions help you to not repeat yourself as much. jQuery uses lamdba functions everywhere, so if you're familiar with jQuery, you should be familiar with the syntax of lambda functions.

In this example, the ajaxed-in content [...]]]></description>
			<content:encoded><![CDATA[<p>Dynamically binding event handlers to content that has been ajaxed in with $.load without repeating yourself can be tricky. Lambda functions help you to not repeat yourself as much. jQuery uses lamdba functions everywhere, so if you're familiar with jQuery, you should be familiar with the syntax of lambda functions.</p>

<p>In this example, the ajaxed-in content contains the elements that trigger a $.load over themselves.</p>

<div class="igBar"><span id="ljavascript-109"><a href="#" onclick="javascript:showPlainTxt('javascript-109'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-109">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">prepare_links <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> month <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.calendar-info .month'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> year <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.calendar-info .year'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.previous-month'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#calendar'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=load"><span style="">load</span></a><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">,</span> prepare_links<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.next-month'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#calendar'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=load"><span style="">load</span></a><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">,</span> prepare_links<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;prepare_links<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Make sure you're not calling the function in the callback by accidentally using prepare_links():</p>

<div class="igBar"><span id="ljavascript-110"><a href="#" onclick="javascript:showPlainTxt('javascript-110'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-110">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.next-month'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#calendar'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=load"><span style="">load</span></a><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span><span style="color: #339933;">,</span> prepare_links<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>; <span style="color: #009966; font-style: italic;">/* Don't do this */</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This would set the callback to whatever the return value of the function is, not to the function itself.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2006/09/sacrificial-lambda/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 32/38 queries in 0.013 seconds using disk

Served from: www.learningjquery.com @ 2010-09-02 23:59:01 -->