<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Learning jQuery</title>
	
	<link>http://www.LEARNINGJQUERY.COM</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Mon, 07 May 2012 12:55:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LearningJquery" /><feedburner:info uri="learningjquery" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>LearningJquery</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Using jQuery’s .pushStack() for reusable DOM traversing methods</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/2-7JbpEHKLg/using-jquerys-pushstack-for-reusable-dom-traversing-methods</link>
		<comments>http://www.LEARNINGJQUERY.COM/2011/12/using-jquerys-pushstack-for-reusable-dom-traversing-methods#comments</comments>
		<pubDate>Tue, 20 Dec 2011 18:51:07 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1324</guid>
		<description>The .pushStack() method has been in jQuery since before version 1.0, but it hasn't received a whole lot of attention outside of core developers and plugin authors. While its usefulness may not be immediately apparent, it can come in really handy in some situations, so I'd like to take a quick look at what it [...]</description>
			<content:encoded><![CDATA[<p>The <code>.pushStack()</code> method has been in jQuery since before version 1.0, but it hasn't received a whole lot of attention outside of core developers and plugin authors. While its usefulness may not be immediately apparent, it can come in really handy in some situations, so I'd like to take a quick look at what it does, how it works, and how we can use it.</p>

  <h4>pushStack Basics </h4>
  <p>At its most basic level, the <code>.pushStack()</code> method accepts an array of DOM elements and "pushes" it onto a "stack" so that later calls to methods like <code>.end()</code> and <code>.andSelf()</code> behave correctly. (Side note: As of jQuery 1.4.2, you can pass in a jQuery object instead of an array, but that isn't documented and jQuery itself always uses an array, so that's what we'll stick to here.)</p>
  <p>Internally, jQuery uses <code>.pushStack()</code> to keep track of the previous jQuery collections as you chain <a href="http://api.jquery.com/category/traversing/">traversing methods</a> such as <code>.parents()</code> and <code>.filter()</code>. This lets us traverse through the DOM, do some stuff, "back up" to previous collections within the same chain using <code>.end()</code>, and then do something else. Here is a somewhat contrived example:</p>
<span id="more-1324"></span>
<div class="igBar"><span id="ljavascript-8"><a href="#" onclick="javascript:showPlainTxt('javascript-8'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-8">
<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;">// select some divs</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;">'div.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;">&nbsp; <span style="color: #006600; font-style: italic;">// find some spans inside those divs and add a class to them</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'baby'</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;">// pop those spans off the &quot;stack&quot;,</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;">// returning to the previous collection (div.container)</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// add a class to the parent of each div.container</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'daddy'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Because <code>.find()</code> returns the result of a <code>.pushStack()</code> call to keep track of the previous collection (as does <code>.parent()</code>), we can use <code>.end()</code> in the above example to return to the container divs.</p>

<h4>Using pushStack for Fun and Profit</h4>
<p>So, this is great for jQuery, but what can <code>.pushStack()</code> do for me and my code? Well, it can help me write specialized DOM traversal plugins that act just like jQuery's own traversal methods. In other words, I can stop chaining the same sets of traversal methods together and instead write a reusable function that still works with with <code>.end()</code> and all that. For example, let's say I often have a need to find an element's grandparent. While I could write <code>$('#myElement').parent().parent()</code> every time, it might be nice to just be able to write <code>$('#myElement').grandparent()</code> instead. A naïve way to write a grandparent plugin would look like this (changing the method name to "grandpa" for this example): </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;"><span style="color: #006600; font-style: italic;">// NOT recommended!</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: #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: #660066;">fn</span>.<span style="color: #660066;">grandpa</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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parents"><span style="">parents</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parents"><span style="">parents</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; <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;">&#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>The problem here is that <em>two</em> new jQuery object instances are added to the stack. So, let's see what happens when we use it:  </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: #006600; font-style: italic;">// The DOM looks like this:</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;">// &lt;div class=&quot;grandpa&quot;&gt;</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;">// &nbsp;&lt;div class=&quot;pa&quot;&gt;</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;">// &nbsp; &nbsp;&lt;div class=&quot;child son&quot;&gt;&lt;/div&gt;</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;">// &nbsp;&lt;/div&gt;</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;">// &lt;/div&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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.son'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">grandpa</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.son'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span> elem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<div class="grandpa">
 <div class="pa">
   <div class="child son"></div>
 </div>
</div>

<p>Without seeing the plugin, we would expect to see <em>"child son"</em> inserted into <code>&lt;div class="son"&gt;</code>, but <em>"pa"</em> is inserted instead. Each <code>.parent()</code> call in the plugin adds to the stack, so using <code>.end()</code> only pops the second one off.</p>
<p>If we use <code>.pushStack()</code> instead, however, we can achieve the expected behavior:</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: #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: #660066;">fn</span>.<span style="color: #660066;">grandma</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;</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> els <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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: 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="/wp-content/themes/ljq/docs-1.7.php?fn=pushStack"><span style="">pushStack</span></a><span style="color: #009900;">&#40;</span> els.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</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: normal; 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;"><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>Within a plugin function, one that is a method of<code> $.fn</code>, the <code>this</code> keyword refers to the jQuery object; therefore, the <code>els</code> variable refers to a jQuery object, as well. To convert it to an array, we use jQuery's <code>.get()</code> method, and we pass that array to <code>.pushStack()</code>. Let's see if <code>.grandma()</code> works any better than <code>.grandpa()</code>.</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;">// The DOM looks like this:</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;">// &lt;div class=&quot;grandma&quot;&gt;</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;">// &nbsp;&lt;div class=&quot;ma&quot;&gt;</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;">// &nbsp; &nbsp;&lt;div class=&quot;child daughter&quot;&gt;&lt;/div&gt;</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;">// &nbsp;&lt;/div&gt;</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;">// &lt;/div&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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.daughter'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">grandma</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.daughter'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span> elem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<div class="grandma">
 <div class="ma">
   <div class="child daughter"></div>
 </div>
</div>

<p>Here, "child daughter" is inserted, which means that <code>.end()</code> works as expected, changing the jQuery collection from the result of <code>.grandma()</code> to the result of <code>$('div.daughter')</code>. So, we've just successfully written a DOM traversal plugin, albeit a very simple one.</p>

<h4>The Simplest DOM Traversal Methods </h4>
<p>If the plugin only uses one DOM traversal method, then <code>.pushStack()</code> isn't really necessary. The <a href="http://www.elijahmanor.com/2011/07/filterbydata-jquery-plugin.html">HTML5 data filter plugin</a> written by Elijah Manor illustrates this point nicely: </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: #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: #660066;">fn</span>.<span style="color: #660066;">filterByData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> type<span style="color: #339933;">,</span> value <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: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.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><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: #000066; font-weight: bold;">return</span> value <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</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; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> type <span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> value <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; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> type <span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</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: #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: 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>Only one new jQuery collection is added to the stack, via <code>.filter()</code>, so using <code>.end()</code> simply pops that one off, and our job is done. </p>

<h4>Filtering grandparents</h4>
<p>For the sake of completeness, it would be nice for this DOM traversal plugin to allow optional "filtering" of the parent and grandparent elements. After all, jQuery's <code>.parent()</code>  and <code>.parents()</code> allow filtering. For example, if I were to write <code>$('div.child').parent('.daddy')</code>, the jQuery collection would only contain an element if <code>div.child</code> had a parent element and if that parent had a class of "daddy."</p>
<p>There are plenty of reasonable ways one could include the filters, but for my purposes I'm going to have a <code>.grandparent()</code> method <em>optionally</em> accept two arguments. If only one argument is provided, it will filter the <em>grandparent</em> element only; if two are provided, the first will filter the parent and the second will filter the grandparent. Here is the full plugin plugin: </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: #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: #660066;">fn</span>.<span style="color: #660066;">grandparent</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> parentFilter<span style="color: #339933;">,</span> grandFilter <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;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>grandFilter <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; grandFilter <span style="color: #339933;">=</span> parentFilter;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; parentFilter <span style="color: #339933;">=</span> undefined;</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></div></li>
<li style="font-weight: normal; 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; <span style="color: #003366; font-weight: bold;">var</span> els <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span> parentFilter <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span> grandFilter <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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=pushStack"><span style="">pushStack</span></a><span style="color: #009900;">&#40;</span> els.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</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: normal; 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;"><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>Finally, we have a nice <code>.grandparent()</code> plugin that adheres to the contract set by other jQuery DOM traversal methods—one that works with both filters and the <code>.end()</code> method. Here is what it could look like in use.</p>

<iframe style="width: 100%; height: 400px" src="http://jsfiddle.net/kswedberg/JSDdb/embedded/js,html,result" allowfullscreen="allowfullscreen" frameborder="0"></iframe><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/2-7JbpEHKLg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2011/12/using-jquerys-pushstack-for-reusable-dom-traversing-methods/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2011/12/using-jquerys-pushstack-for-reusable-dom-traversing-methods</feedburner:origLink></item>
		<item>
		<title>Using jQuery’s Data APIs</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/9snCrfVEkeY/using-jquerys-data-apis</link>
		<comments>http://www.LEARNINGJQUERY.COM/2011/09/using-jquerys-data-apis#comments</comments>
		<pubDate>Fri, 02 Sep 2011 16:52:09 +0000</pubDate>
		<dc:creator>Dave Methvin</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1346</guid>
		<description>In the beginning (well, beginning with jQuery 1.2.3 in early 2008) there was the jQuery.data() API. It offers a way to associate JavaScript data &amp;#8212; strings, numbers, or any object &amp;#8212; with a DOM element. As long as you manipulate the DOM element with jQuery, the library ensures that when the DOM element goes away, [...]</description>
			<content:encoded><![CDATA[<p>In the beginning (well, beginning with jQuery 1.2.3 in early 2008) there was the <a href="http://api.jquery.com/jQuery.data"><code>jQuery.data()</code></a> API. It offers a way to associate JavaScript data &#8212; strings, numbers, or any object &#8212; with a DOM element. As long as you manipulate the DOM element with jQuery, the library ensures that when the DOM element goes away, the associated data goes away as well. This is especially important for older versions of IE that tend to leak memory when JavaScript data is mixed with DOM data.</p>

<p>Most jQuery code sets data values using the higher-level <a href="http://api.jquery.com/data"><code>.data()</code></a> API; for example, <code>$("div").data("imaDiv", true)</code> sets a boolean value on every <code>div</code> in the document. This API, in turn, calls down to <code>jQuery.data()</code> with each element to set the value. For completeness, there are also <a href="http://api.jquery.com/jQuery.removeData"><code>jQuery.removeData()</code></a> and <a href="http://api.jquery.com/removeData"><code>.removeData()</code></a> to remove data elements, and <a href="http://api.jquery.com/jQuery.hasData"><code>jQuery.hasData()</code></a> to determine if any data is currently set for an element.</p>
<span id="more-1346"></span>
<p>So to recap: At the inception of these APIs, they were <em>only</em> about getting and setting values associated with DOM elements in memory. Most importantly, the data was managed to ensure no memory would leak when the DOM elements were removed. Many internal jQuery features such as event handling and toggle state memory use these data APIs and their benefits.</p>

<h4>Enter HTML5</h4>

<p>A few years later, HTML5 became popular and associated another concept with the word "data" through its <a href="http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes"><code>data-*</code></a> attributes and the associated DOM <a href="http://www.w3.org/TR/html5/elements.html#dom-dataset"><code>.dataset</code></a> property. This isn't quite the same as jQuery's original idea of data: It involves values being associated with HTML elements in markup and not DOM elements in memory. But they are logically close enough that we added the ability to read HTML5 <code>data-*</code> attributes into jQuery's data object starting with version 1.4.</p>

<p>It's not a perfect marriage, though. HTML5 <code>data-*</code> attribute names are more like CSS names; a name like <code>data-shrivel-up</code> is turned into <code>shrivelUp</code> when read in JavaScript-land. No such rules ever applied to jQuery data names in the past, which means we may have to try both <code>shrivel-up</code> and <code>shrivelUp</code> to find a match. We know it's not ideal, but it's a consequence of trying to fit two concepts with differing semantics into a single API.</p>

<h4>Rules of the Road for Data APIs</h4>

<p>With that history in mind, there are a few important things you should know in order to use the <code>.data()</code> and <code>jQuery.data()</code> APIs effectively. To give you a better sense of what's going on, the items are illustrated with some code. Assume that each code block runs independently of the others and that they all refer the following HTML:</p>

<div class="igBar"><span id="lhtml-21"><a href="#" onclick="javascript:showPlainTxt('html-21'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-21">
<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;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;novel&quot;</span> data-novelist<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'{&quot;firstname&quot;: &quot;Jose&quot;, &quot;lastname&quot;: &quot;Saramago&quot;}'</span><span style="color: #000000;">&gt;</span></span>Blindness<span style="color: #009900;"><span style="color: #000000;">&lt;/div&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;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;poem&quot;</span> data-poet<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Edna St. Vincent Millay&quot;</span><span style="color: #000000;">&gt;</span></span>Sonnet 18<span style="color: #009900;"><span style="color: #000000;">&lt;/div&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;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;story&quot;</span> data-story-writer<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Raymond Carver&quot;</span><span style="color: #000000;">&gt;</span></span>A Small, Good Thing<span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Here are the rules of the road:</p>

<ol>
<li>
  <h5>Only the <code>.data()</code> API reads HTML5 <code>data-*</code> attributes, and it does so <em>once</em>.</h5>
 <p>The in-memory data object for an element is initialized from those <code>data-*</code> attributes the first time you call <code>.data()</code> for the element. Any subsequent changes to the attributes are ignored, since jQuery has already cached the data. </p>
<p><strong>Rule:</strong> If HTML5 <code>data-*</code> attributes change during program execution, use jQuery's <code>.attr()</code> method to get the current values.</p>
<div class="igBar"><span id="ljavascript-22"><a href="#" onclick="javascript:showPlainTxt('javascript-22'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-22">
<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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.data"><span style="">$.<span style="color: #660066;">data</span></span></a><span style="color: #009900;">&#40;</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poem'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'poet'</span> <span style="color: #009900;">&#41;</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;">//&gt;&gt; undefined</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Change the HTML5 data-poet attribute</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;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-poet'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Edmund Spenser'</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</span></div></li>
</ol></div>
</div></div><br />
</li>


<li>
  <h5>The <code>.data()</code> API converts HTML5 <code>data-*</code> values to Javascript types whenever possible.</h5>
<p>That means sequences of digits or exponential-looking values like <code>"11E5"</code> are translated to a Javascript Number type, the string <code>"true"</code> becomes Boolean <code>true</code>, and a valid JSON string becomes a JavaScript object.</p>
<p><strong>Rule:</strong> To get HTML5 <code>data-*</code> attributes as strings without data conversion, use jQuery's <code>.attr()</code> method.</p>
<div class="igBar"><span id="ljavascript-23"><a href="#" onclick="javascript:showPlainTxt('javascript-23'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-23">
<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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#novel'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'novelist'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; Object&gt; {&quot;firstname&quot;: &quot;Jose&quot;, &quot;lastname&quot;: &quot;Saramago&quot;}</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#novel'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-novelist'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; '{&quot;firstname&quot;: &quot;Jose&quot;, &quot;lastname&quot;: &quot;Saramago&quot;}'</span></div></li>
</ol></div>
</div></div><br />
</li>

<li>
  <h5>The lower-level <code>jQuery.data()</code> API does not read HTML5 <code>data-*</code> attributes.</h5>
 <p>However, if the <code>.data()</code> API has been called already on that DOM element, <code>jQuery.data()</code> will "see" the values that it has already read from the <code>data-*</code> attributes. Conversely, if <code>jQuery.data()</code> sets a value with the same name as an HTML5 <code>data-*</code> attribute and <code>.data()</code> later reads them, the HTML5 attribute is <em>ignored</em>.</p>
<p><strong>Rule:</strong> To prevent confusion, do not use similar names for HTML5 <code>data-*</code> attributes and strictly internal data stored using <code>jQuery.data()</code> or <code>.data()</code> on the same elements.</p>
<div class="igBar"><span id="ljavascript-24"><a href="#" onclick="javascript:showPlainTxt('javascript-24'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-24">
<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;">// Before reading with .data()</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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.data"><span style="">$.<span style="color: #660066;">data</span></span></a><span style="color: #009900;">&#40;</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poem'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'poet'</span> <span style="color: #009900;">&#41;</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;">//&gt;&gt; undefined</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// After reading with .data()</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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.data"><span style="">$.<span style="color: #660066;">data</span></span></a><span style="color: #009900;">&#40;</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poem'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'poet'</span> <span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</span></div></li>
</ol></div>
</div></div><br />
</li>

<li>
  <h5><em>No</em> jQuery data API ever <em>changes</em> HTML5 <code>data-*</code> attributes.</h5>
<p> Most uses of <code>.data()</code> and <code>.removeData()</code> are still for the original purpose of associating data with DOM elements in memory. Updating DOM attributes each time data was changed would slow things down for no good reason. Also, it's not even possible to serialize all data types that might be attached to a DOM element, such as functions, references to other DOM elements, or custom JavaScript objects. </p>
<p><strong>Rule:</strong> To update or remove HTML5 <code>data-*</code> attributes, use jQuery's <code>.attr()</code> or <code>.removeAttr()</code> methods.</p>
<div class="igBar"><span id="ljavascript-25"><a href="#" onclick="javascript:showPlainTxt('javascript-25'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-25">
<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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Change the HTML5 data-* attribute</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;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-poet'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'William Shakespeare'</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edna St. Vincent Millay&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;William Shakespeare&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Change .data('poet')</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;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Edmund Spenser'</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Edmund Spenser&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#poem'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-poet'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;William Shakespeare&quot;</span></div></li>
</ol></div>
</div></div><br />
</li>


<li>
  <h5>All <code>data-*</code> names are stored in camelCase in the jQuery data object, using <a href="http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes">W3C rules</a>.</h5>
<p> So, <code>data-caMEL-case</code> becomes the <code>camelCase</code> property in the data object and should be accessed using <code>.data("camelCase")</code>. Because many people will use <code>.data("camel-case")</code> instead, we convert that to <code>camelCase</code> as well, but only if no data item named <code>camel-case</code> is found so it's faster to use the first form. If you get the entire data object using code like <code>data = jQuery.data(elem)</code>, you <em>must</em> use <code>data.camelCase</code> to access the data item.</p>
<p><strong>Rule:</strong> When accessing data taken from <code>data-*</code> attributes, and especially when accessing the data object directly, use the W3C camelCasing conventions.</p>
<div class="igBar"><span id="ljavascript-26"><a href="#" onclick="javascript:showPlainTxt('javascript-26'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-26">
<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;">// Not recommended:</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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#story'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'STORY-writer'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Raymond Carver&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Better:</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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#story'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'storyWriter'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Raymond Carver&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Broken:</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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#story'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dataStoryWriter'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; undefined</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Better:</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;">console</span>.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#story'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-STORY-writer'</span><span style="color: #009900;">&#41;</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;">//&gt;&gt; &quot;Raymond Carver&quot;</span></div></li>
</ol></div>
</div></div><br />
</li>
</ol>

<h4>Pick What You Like</h4>

<p>Over time, jQuery's <code>.data()</code> API has taken on more responsibilities than it originally had when it was just a way to associate in-memory data with DOM elements and prevent IE leakage. If you need only a simple way to read HTML5 <code>data-*</code> attributes as strings, then the <code>.attr()</code> method may be the best choice, even though the siren-song-name <code>.data()</code> may be telling you otherwise. Whether you use <code>.attr()</code> or <code>.data()</code>, they work consistently across browsers all the way back to IE6 &#8212; even if the browser doesn't support HTML5 &#8212; so just choose the API with the feature set that works best for your needs.</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/9snCrfVEkeY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2011/09/using-jquerys-data-apis/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2011/09/using-jquerys-data-apis</feedburner:origLink></item>
		<item>
		<title>jQuery.map() in 1.6</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/iXIHWpOZDz0/jquery-map-in-16</link>
		<comments>http://www.LEARNINGJQUERY.COM/2011/05/jquery-map-in-16#comments</comments>
		<pubDate>Tue, 03 May 2011 20:39:43 +0000</pubDate>
		<dc:creator>Jordan Boesch</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1287</guid>
		<description>Among all of the great fixes and additions to jQuery 1.6, I'm happy to say that jQuery.map() now supports objects! The previous map only supported arrays. With other libraries already offering object support for map, it was a nice addition. Let's say you want to collect an array of object keys from a JSON object. [...]</description>
			<content:encoded><![CDATA[<p>
Among all of the <a href="http://blog.jquery.com/2011/05/03/jquery-16-released/" target="_blank">great fixes and additions</a> to jQuery 1.6, I'm happy to say that <a href="http://api.jquery.com/jquery.map/" target="_blank">jQuery.map()</a> now supports objects!  The previous map only supported arrays. With <a href="http://documentcloud.github.com/underscore/#map" target="_blank">other libraries</a> already offering object support for map, it was a nice addition.</p><span id="more-1287"></span>
<p>
Let's say you want to collect an array of object keys from a JSON object.  
</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> myObj <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; <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jordan&quot;</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; <span style="color: #3366CC;">&quot;hair_color&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;brown&quot;</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; <span style="color: #3366CC;">&quot;eye_color&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ravishing&quot;</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>Here's how you <em>would</em> have done it in older versions of jQuery (prior to 1.6):</p>

<div class="igBar"><span id="ljavascript-39"><a href="#" onclick="javascript:showPlainTxt('javascript-39'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-39">
<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> objKeys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span> myObj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> key<span style="color: #339933;">,</span> value <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; objKeys.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> key <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>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// objKeys == [ &quot;name&quot;, &quot;hair_color&quot;, &quot;eye_color&quot; ]</span></div></li>
</ol></div>
</div></div><br />

<p>Here's the new way (just a little more convenient):</p>

<div class="igBar"><span id="ljavascript-40"><a href="#" onclick="javascript:showPlainTxt('javascript-40'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-40">
<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> objKeys <span style="color: #339933;">=</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.map"><span style="">$.<span style="color: #660066;">map</span></span></a><span style="color: #009900;">&#40;</span> myObj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> value<span style="color: #339933;">,</span> key <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> key;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// objKeys == [ &quot;name&quot;, &quot;hair_color&quot;, &quot;eye_color&quot; ]</span></div></li>
</ol></div>
</div></div><br />

<h4>Going inside the new jQuery.map()</h4>
<p>
For those that are curious and a little more advanced, there are some neat things going on under the hood of the new <a href="http://api.jquery.com/jQuery.map/" target="_blank">jQuery.map()</a> that I would like to talk about.
</p>
<p>
Adding object support seems pretty trivial at first since <a href="http://api.jquery.com/jQuery.each/" target="_blank">jQuery.each()</a> is already doing it - so it must be an easy patch, right? Well, not really. Let's look at how jQuery.each() is doing it.  If you <a href="https://github.com/jquery/jquery/blob/master/src/core.js#L591" target="_blank">look at the jQuery source</a> on github, you'll see that it's doing: 
</p>

<div class="igBar"><span id="ljavascript-41"><a href="#" onclick="javascript:showPlainTxt('javascript-41'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-41">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">length <span style="color: #339933;">=</span> object.<span style="color: #660066;">length</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">isObj <span style="color: #339933;">=</span> length <span style="color: #339933;">===</span> undefined <span style="color: #339933;">||</span> jQuery.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span> object <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>
Can you see the flaw in this? It's going to treat the variable <em>object</em> (could be an array or object) like an object if length is undefined.  What happens when I have an object with a "length" property?  <a href="http://jsfiddle.net/jboesch26/r5aAS/" target="_blank">It dies a horrible death</a>.  Some have reported this <a href="http://bugs.jquery.com/ticket/7260" target="_blank">issue</a>.
</p>
<p>
For the new jQuery.map(), we wanted support for objects and <em>also</em> be able to pass an object with a length property and not have it blow up like jQuery.each() does. <a href="https://github.com/jquery/jquery/pull/299/files#L0R713" target="_blank">Dan Heberden came to the rescue</a>. <a href="http://danheberden.com/" target="_blank">Dan</a> spent some time making sure that jQuery.map() didn't face the same fate while keeping performance in mind.
</p>
<p>
Here is what Dan did to see if <em>elems</em> is an array:
</p>
<div class="igBar"><span id="ljavascript-42"><a href="#" onclick="javascript:showPlainTxt('javascript-42'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-42">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">length <span style="color: #339933;">=</span> elems.<span style="color: #660066;">length</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">isArray <span style="color: #339933;">=</span> elems <span style="color: #000066; font-weight: bold;">instanceof</span> jQuery <span style="color: #339933;">||</span> length <span style="color: #339933;">!==</span> undefined &#038;& <span style="color: #000066; font-weight: bold;">typeof</span> length <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;number&quot;</span> &#038;& <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> length<span style="color: #339933;">&gt;</span> <span style="color: #CC0000;color:#800000;">0</span> &#038;& elems<span style="color: #009900;">&#91;</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#93;</span> &#038;& elems<span style="color: #009900;">&#91;</span> length <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> length <span style="color: #339933;">===</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #339933;">||</span> jQuery.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span> elems <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ;</div></li>
</ol></div>
</div></div><br />

<p>I should note that John Resig <a href="https://github.com/jquery/jquery/commit/6c449fd5df3e0ec50e893d055da9aea486e7d71c/" target="_blank">added</a>:</p>

<div class="igBar"><span id="ljavascript-43"><a href="#" onclick="javascript:showPlainTxt('javascript-43'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-43">
<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: #339933;">||</span> length <span style="color: #339933;">===</span> <span style="color: #CC0000;color:#800000;">0</span></div></li>
</ol></div>
</div></div><br />

<p>To support <a href="http://bugs.jquery.com/ticket/8993" target="_blank">empty node lists</a>.</p>

<p>
Let's look at the first part of our isArray variable.  You'll notice it does a check for:
</p>
<div class="igBar"><span id="ljavascript-44"><a href="#" onclick="javascript:showPlainTxt('javascript-44'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-44">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">elems <span style="color: #000066; font-weight: bold;">instanceof</span> jQuery</div></li>
</ol></div>
</div></div><br />
<p>
We're checking to see if <em>elems</em> is an instanceof jQuery.  We're doing this first because you're most likely dealing with a <a href="http://api.jquery.com/category/properties/jquery-object-instance-properties/" target="_blank">jQuery collection/array-like object</a>. Since jQuery collections are treated like regular arrays (uses for loop), this is an optimized way of checking if we should treat <em>elems</em> like an array.  Below is an example of <a href="http://api.jquery.com/map/" target="_blank">jQuery.fn.map()</a> in action.
</p>
<div class="igBar"><span id="ljavascript-45"><a href="#" onclick="javascript:showPlainTxt('javascript-45'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-45">
<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="/wp-content/themes/ljq/docs-1.7.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> i<span style="color: #339933;">,</span> element <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: #006600; font-style: italic;">// under the hood, this call to map passes the </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;">// isArray variable check right away because </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;">// $('div') is an instance of jQuery</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;">// using 'this' here refers to the DOM element</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>
<a href="http://jsfiddle.net/jboesch26/aBkmr/" target="_blank">Here is</a> jQuery.map() with a jQuery collection passed as the first argument:
</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;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.map"><span style="">$.<span style="color: #660066;">map</span></span></a><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> element<span style="color: #339933;">,</span> i <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: #006600; font-style: italic;">// does the same as above and will pass the &quot;instanceof&quot; check</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;">// also, note that the arguments are backwards</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;">// using 'this' here refers to the window object</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>If what you passed is not an instance of jQuery but it passed the isArray variable check, it means your call to the jQuery.map() utility function probably looked something 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;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.map"><span style="">$.<span style="color: #660066;">map</span></span></a><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;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">5</span><span style="color: #009900;">&#93;</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><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>In this case it falls back to this rigorous check to see if it's an array. 
<strong>Note:</strong> I'm breaking it down into separate lines so it's easier to understand.
</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: #006600; font-style: italic;">// make sure that we actually have a length property</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">length <span style="color: #339933;">!==</span> undefined</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;">// if it's a number, it could possibly be an array</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;">// but still needs some more checking</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &#038;& <span style="color: #000066; font-weight: bold;">typeof</span> length <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;number&quot;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &#038;& <span style="color: #009900;">&#40;</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;">// make sure we're dealing with a set of non-empties</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: #009900;">&#40;</span> length<span style="color: #339933;">&gt;</span> <span style="color: #CC0000;color:#800000;">0</span> &#038;& elems<span style="color: #009900;">&#91;</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#93;</span> &#038;& elems<span style="color: #009900;">&#91;</span> length <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</span> <span style="color: #009900;">&#93;</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; <span style="color: #006600; font-style: italic;">// if we're dealing with an empty node list - this is rare</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: #339933;">||</span> length <span style="color: #339933;">===</span> <span style="color: #CC0000;color:#800000;">0</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;">// this check is a last resort and only gets hit if we </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;">// pass an array like $.map(new Array(1), fn);</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: #339933;">||</span> jQuery.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span> elems <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;">&#41;</span></div></li>
</ol></div>
</div></div><br />

<p>The rest of the logic is pretty straight forward - just some for loops and super fun for-in loops. </p>

<p>Hope you enjoy jQuery.map() with object support!</p>
<img src="http://feeds.feedburner.com/~r/LearningJquery/~4/iXIHWpOZDz0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2011/05/jquery-map-in-16/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2011/05/jquery-map-in-16</feedburner:origLink></item>
		<item>
		<title>Merging jQuery Deferreds and .animate()</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/I3AWCnf18hU/merging-jquery-deferreds-and-animate</link>
		<comments>http://www.LEARNINGJQUERY.COM/2011/02/merging-jquery-deferreds-and-animate#comments</comments>
		<pubDate>Thu, 17 Feb 2011 12:46:15 +0000</pubDate>
		<dc:creator>Dan Heberden</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Effects]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1261</guid>
		<description>Editor's Note: This article originally appeared on danheberden.com. jQuery&amp;#8217;s .animate() method, and the shorthand methods that use it, are fantastic tools to create animations. Creating animations that link together to achieve a particular effect, and do something specific at the end of the animation, can be a painful, messy task. Luckily, we have .queue() for [...]</description>
			<content:encoded><![CDATA[<p><i>Editor's Note: This article originally appeared on <a href="http://danheberden.com/merging-jquery-deferreds-and-animate-2011-02-13/">danheberden.com</a>.</i></p>
 <p>jQuery&#8217;s <code>.animate()</code> method, and the shorthand methods that use it, are fantastic tools to create animations. Creating animations that link together to achieve a particular effect, and do something specific at the end of the animation, can be a painful, messy task. Luckily, we have <code>.queue()</code> for mashing animations together.</p> 
<p>But what happens when you want to bridge the gap between ajax requests and animating? When you want to queue a bunch of animations, get data from the server, and handle it all at once, without a crap-load of nested callbacks? That&#8217;s when <code>jQuery.Deferred()</code> puts on its cape, tightens its utility belt, and saves the day.</p> <span id="more-1261"></span>
<h4>Disclaimer</h4> 
<p>I should note, however, that this is more-or-less giving an example of a pending feature request to add deferreds support in <code>$.fn.animate</code>. If the feature request is accepted and landed, it won&#8217;t show up until version 1.6 of jQuery. The principles, however, speak to jQuery&#8217;s flexibility and how to forge its multitude of great features into an even stronger tool.</p> 
<p>While this works, its behavior isn&#8217;t consistent with that of jQuery. Namely, the new custom animate method doesn&#8217;t return &#8216;this&#8217;, but a Deferred object. However, that&#8217;s kind of the point of <code>$.sub()</code>: allowing you to copy the jQuery object and have your way with it. So, <em>do</em> try this at home &#8211; just don&#8217;t threaten my life if your site explodes.</p> 
<h4>The Demo</h4> 
<p>The following demo is a basic, distilled use-case for this kind of situation. Clicking the button opens a div that contains a loading message. While it&#8217;s opening, it is also querying the server for information to populate the box. Once <strong>both</strong> have finished, the loading div is hidden and the box with the retrieved data remains.<br /> 
<iframe style="width: 100%; height: 190px" src="http://jsfiddle.net/danheberden/NMh7c/embedded/result,js,html,css"></iframe></p> 
<h4>Modifying .animate()</h4> 
<p>Here is the code driving the change to <code>.animate()</code></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: #006600; font-style: italic;">// create a sub of jquery (Basically, a copy we can mess with)</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> my$ <span style="color: #339933;">=</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.sub"><span style="">$.<span style="color: #660066;">sub</span></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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// make my$ have a modified animate function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">my$.<span style="color: #660066;">fn</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> props<span style="color: #339933;">,</span> speed<span style="color: #339933;">,</span> easing<span style="color: #339933;">,</span> callback <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;">// from jQuery.speed, forces arguments into props and options objects</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> options <span style="color: #339933;">=</span> speed &amp;&amp; <span style="color: #000066; font-weight: bold;">typeof</span> speed <span style="color: #339933;">===</span> &quot;object&quot; <span style="color: #339933;">?</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> speed<span style="color: #009900;">&#41;</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; complete<span style="color: #339933;">:</span> callback <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>callback &amp;&amp; easing <span style="color: #339933;">||</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; jQuery.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span> speed <span style="color: #009900;">&#41;</span> &amp;&amp; speed<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; duration<span style="color: #339933;">:</span> speed<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; easing<span style="color: #339933;">:</span> callback &amp;&amp; easing <span style="color: #339933;">||</span> easing &amp;&amp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #339933;">!</span>jQuery.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>easing<span style="color: #009900;">&#41;</span> &amp;&amp; easing</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: normal; 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;">// create the deferred</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> dfd <span style="color: #339933;">=</span> my$.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</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;">// a copy of the complete callback</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; complete <span style="color: #339933;">=</span> options.<span style="color: #660066;">complete</span><span style="color: #339933;">,</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;">// and the count of how many items</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; count <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">length</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// make a new complete function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; options.<span style="color: #660066;">complete</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: #006600; font-style: italic;">// that calls the old one if it exists</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; complete &amp;&amp; complete.<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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// and decrements count and checks if it's 0</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;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!--</span>count <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: #006600; font-style: italic;">// and when it is, resolves the DFD</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; dfd.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=resolve"><span style="">resolve</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; <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: #009900;">&#125;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// all the hooks have been made, call the regular animate</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; jQuery.<span style="color: #660066;">fn</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> props<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;</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;">// return the promise that we'll do something</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> dfd.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=promise"><span style="">promise</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;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br /> 
<p>While the comments explain just about everything, be sure to read up on <code>$.sub()</code> if you haven&#8217;t already. The new animate function on <code>my$</code> simulates the method signature of the function, <code>$.fn.animate</code>, it&#8217;s attempting to replace. In short, <code>speed</code>, <code>easing</code>, and <code>callback</code> are forced into an options object &mdash; the same as if the second parameter was an object.</p> 
<p>The deferred is created, a copy of the complete callback, and the count of how many items. Why? We want to fire the <code>resolve()</code> function on the deferred once <em>all</em> of the animations have finished. The <code>complete()</code> callback is replaced with a wrapper function that calls the original callback and decrements and checks the count. When the count reaches zero, all items have been animated and it&#8217;s safe to fire the <code>resolve()</code> function.</p> 
<p>The untouched, standard version of <code>$.fn.animate</code> is called with the same &#8216;this&#8217;, properties, and the modified options object with the new complete wrapper function.</p> 
<p>Returned is the promise, <code>dfd.promise()</code>, that lets <code>$.when()</code> do its awesomeness.</p> 
<h4>Putting it into action</h4> 
<p>If you haven&#8217;t familiarized yourself with deferreds, I highly recommend you read <a href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/" target="eric">Eric Hynds&#8217; fantastic article about it</a>.</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="color: #006600; font-style: italic;">// retrieves content and updates a dom element</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 a promise</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;">function</span> populateBox<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: #000066; font-weight: bold;">return</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.ajax"><span style="">$.<span style="color: #660066;">ajax</span></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; url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'your/server/url'</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; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; type<span style="color: #339933;">:</span> &quot;POST&quot;<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; success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</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; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slideDown"><span style="">slideDown</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a> <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: #009900;">&#125;</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: #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: normal; 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;">// the &quot;Get Message&quot; click handler</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;">'button.load'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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;</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;">// save the button, box and loading as my$ objects</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> $button <span style="color: #339933;">=</span> my$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $box <span style="color: #339933;">=</span> my$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $loading <span style="color: #339933;">=</span> my$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.loading'</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// when the functions are done</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.when"><span style="">$.<span style="color: #660066;">when</span></span></a><span style="color: #009900;">&#40;</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;">// $box was created with my$, so it will</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;">// use the custom animate function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $box.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slideDown"><span style="">slideDown</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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; populateBox<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; <span style="color: #006600; font-style: italic;">// then run the 1st function on success</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;">// and the second function if either fails</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=then"><span style="">then</span></a><span style="color: #009900;">&#40;</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;">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: #006600; font-style: italic;">// remove loading, we're done</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $loading.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slideUp"><span style="">slideUp</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; <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; <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: #006600; font-style: italic;">// get that button back here</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $button.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=show"><span style="">show</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;">// and hide the box</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $box.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slideUp"><span style="">slideUp</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; <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: #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>Because the new animate function was created on <code>my$</code>, the copy of jQuery made using <code>$.sub()</code>,  <code>.hide()</code>, <code>.slideUp()</code>, and other helper functions that use the custom <code>.animate()</code> function.</p> 
<p>When the animation and ajax request both succeed, thus resolving the promise as <code>true</code>, the first callback function of <code>$.then()</code> is called. However, if one of them fails, the second will be called. You can re-run the demo (by clicking on the run button) and opt to fail the ajax request to see the second function get run. The <code>.then()</code> function is a handy mix of <code>.done()</code> and <code>.fail()</code>, which are useful if you want to provide multiple callbacks.</p> 
<h4>Summary</h4> 
<p>While I highly doubt this will be <em>the</em> solution to using deferreds with <code>.animate()</code>, I&#8217;m confident it&#8217;ll get the ball rolling. Too, it covers some other topics, such as <code>$.sub()</code>, deferreds, and wrapping functions with alternate behavior, that hopefully you found interesting.</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/I3AWCnf18hU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2011/02/merging-jquery-deferreds-and-animate/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2011/02/merging-jquery-deferreds-and-animate</feedburner:origLink></item>
		<item>
		<title>Introducing jQuery API Search</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/v2nHIDXFeWI/introducing-jquery-api-search</link>
		<comments>http://www.LEARNINGJQUERY.COM/2010/09/introducing-jquery-api-search#comments</comments>
		<pubDate>Fri, 10 Sep 2010 11:28:26 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1240</guid>
		<description>Half-baked tutorials and plugins have been stacking up for months in my virtual kitchen, waiting for me to fire up the oven, finish the cooking, and spread them out on the table. For some reason, though, I've become less and less sure about whether I've put all the right ingredients into the mix. It's irritating, [...]</description>
			<content:encoded><![CDATA[<p>Half-baked tutorials and plugins have been stacking up for months in my virtual kitchen, waiting for me to fire up the oven, finish the cooking, and spread them out on the table. For some reason, though, I've become less and less sure about whether I've put all the right ingredients into the mix. It's irritating, to be sure, but I'm tired of fretting about it. I'm going to consider this the first of what I hope to be many "taste tests" — experiments in various degrees of completion thrown against the wall to see what, if anything, sticks.</p>
<p>As some of you may know, the online jQuery documentation went through a major overhaul in January of this year, coinciding with the release of jQuery 1.4. Packt Publishing "open sourced" the jQuery 1.4 Reference Guide that Jonathan Chaffer and I had been writing, allowing us to put its entire contents (and more) on <a href="http://api.jquery.com/">api.jquery.com</a>. Some of you may also know that <a href="http://api.jquery.com/api/">the raw XML content of the site is available as a single file</a>, which has allowed other sites such as <a href="http://jqapi.com">jqapi.com</a> and <a href="http://idocs.brandonaaron.net">idocs.brandonaaron.net</a> to provide alternative views of that content. But what most of you probably do <em>not</em> know is that the jQuery API has been available for quite some time as a searchable API that returns the results in JSON format.</p>
<span id="more-1240"></span>
<p><strong>Note:</strong> The <em>reason</em> you probably don't know about it is that it's half-baked. So, please go easy on it until some smart people have a chance to look under the hood at what I'm doing. Crazy abuse will surely bring it to its knees.</p>
<h4>Motivation</h4>
<p>I wanted to let people access the jQuery documentation via JavaScript from any other site and pull out exactly what is needed. I also wanted to play around with JSONP. This fits both of those desires.</p>

<h4>URL</h4>
<p>To access the searchable API, use the following URL:</p>
<p>http://api.jquery.com/jsonp/</p>

<h4>Examples</h4>
<p>You can tap into the API with one of jQuery's Ajax methods. If you use <code>$.get()</code> or <code>$.getJSON()</code>, you'll need to append "?callback=?" to the URL. With <code>$.get()</code>, you'll also need to set the <code>dataType</code> argument to "jsonp". </p>
<p><i>Find entries in the API that have "class" in their name and then do something with them:</i>  </p>
<div class="igBar"><span id="ljavascript-57"><a href="#" onclick="javascript:showPlainTxt('javascript-57'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-57">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.get"><span style="">$.<span style="color: #660066;">get</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://api.jquery.com/jsonp/?callback=?'</span><span style="color: #339933;">,</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'class'</span><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; <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</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: #006600; font-style: italic;">// do something with data</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: #339933;">,</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #3366CC;">'jsonp'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p><i>Find all effects-category entries and then do something with them: </i>  </p>
<div class="igBar"><span id="ljavascript-58"><a href="#" onclick="javascript:showPlainTxt('javascript-58'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-58">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.getJSON"><span style="">$.<span style="color: #660066;">getJSON</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://api.jquery.com/jsonp/?callback=?'</span><span style="color: #339933;">,</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#123;</span>category<span style="color: #339933;">:</span> <span style="color: #3366CC;">'effects'</span><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; <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<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: #006600; font-style: italic;">// do something with json</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>You can, of course, also use the low-level <code>$.ajax()</code> method.   </p>
<p><i>Find entries with a title that ends in "ajax"; append a link for each to the document body:</i>
</p>      
<div class="igBar"><span id="ljavascript-59"><a href="#" onclick="javascript:showPlainTxt('javascript-59'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-59">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.ajax"><span style="">$.<span style="color: #660066;">ajax</span></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; url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://api.jquery.com/jsonp/'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jsonp'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'ajax'</span><span style="color: #339933;">,</span> match<span style="color: #339933;">:</span> <span style="color: #3366CC;">'end'</span><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; success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>json.<span style="color: #660066;">length</span>; i<span style="color: #339933;">&lt;</span>len ; i<span style="color: #339933;">++</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> entry <span style="color: #339933;">=</span> json<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</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;">'&lt;a /&gt;'</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; &nbsp; &nbsp; href<span style="color: #339933;">:</span> entry.<span style="color: #660066;">url</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; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #339933;">:</span> entry.<span style="color: #660066;">title</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>.<a href="/wp-content/themes/ljq/docs-1.7.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: normal; 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; <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;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>Search Types</h4>
<p>The JSONP API is searchable by name, category, and version. Searching by more than one criterion returns results that match <em>all</em> of the criteria. <strong>All searches are case-insensitive.</strong></p>

<ul>
  <li><h5>Search by name:</h5>
    <ul>
      
      <li><strong>Key</strong>: title</li>
      <li><strong>Value</strong>: the name of any jQuery method, property, or selector</li>
      <li><strong>Searches in</strong>: post title and post slug</li>
      <li><strong>Substitutions</strong>: Before querying the database, all "$" are converted to "jQuery" and each instance of one or more spaces is converted to a hyphen ("-") for both the search values and the the post title and slug.</li>
      <li><strong>Default</strong>: all titles</li>
    </ul>
  </li>
  
  <li><h5>Search by category name</h5>
    <ul>
      <li><strong>Key</strong>: category</li>
      <li><strong>Value</strong>: category name</li>
      <li><strong>Substitutions</strong>: Before querying the database, each instance of one or more spaces is converted to a hyphen ("-")</li>
      <li><strong>Searches in</strong>: category slug</li>
      <li><strong>Default</strong>: all categories</li>
    </ul>
  </li>
  <li>
  <h5>Search by version number</h5>
    <ul>
    
      <li><strong>Key</strong>: version</li>
      <li><strong>Value</strong>: a jQuery version number</li>
      <li><strong>Searches in</strong>: category slug for all categories that are a child of the main "version" category</li>
      <li><strong>Default</strong>: all versions</li>
    </ul>
    
  </li>
</ul>
<h4>String Matching</h4>
  <p>The "match" parameter lets you be confine the results to those that match the entire search term, or just the start or end of it.  </p>
<ul>
  <li><strong>Key</strong>: match</li>
  <li><strong>Value</strong>: one of "start", "end", or "exact" (for search by version number, one of "end" or "exact")</li>
  <li><strong>Default</strong>: "anywhere", except for version number, which has a default of "start"</li>
</ul>

<h4>Returned Data</h4>
<p>Data is returned as an array of objects. Each item in the array is an object representing a single method, property, or selector. For example, a result with one item would have the following structure:</p>

<div class="igBar"><span id="ljavascript-60"><a href="#" onclick="javascript:showPlainTxt('javascript-60'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-60">
<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;">&#91;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <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: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</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; <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</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; <span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// &quot;method&quot;, &quot;property&quot;, or &quot;selector&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">&quot;signatures&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <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: #3366CC;">&quot;added&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;...&quot;</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; <span style="color: #3366CC;">&quot;params&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</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;">&#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: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</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; <span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</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; <span style="color: #3366CC;">&quot;optional&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// either &quot;true&quot; or an empty string</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: #3366CC;">&quot;desc&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span> <span style="color: #006600; font-style: italic;">// description of the parameter</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></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#93;</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></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#93;</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; <span style="color: #3366CC;">&quot;desc&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// short description</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">&quot;longdesc&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// long description</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">&quot;return&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span> <span style="color: #006600; font-style: italic;">// type of return value</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#93;</span></div></li>
</ol></div>
</div></div><br />
<p>Note that currently the examples/demos from each entry are not returned.</p>
<h4>A (Fairly) Basic Demo</h4>
<p>I put together a quick demo to give an idea of the API's flexibility. Type something into the "Name" field in the form below—say, "ajax"—and watch as the results come back. Inside the "Advanced" area, choose the "Matching... <strong>End</strong>" radio button and search again to see how the results differ.  </p>
<div class="jqas">
  <form id="jqas" action="" class="group">
    <div>
      <a id="search-again" class="js-hide btn" href="#">&#x271A; Search Again</a>
      <h3>jQuery API Search <span class="note">A demonstration</span></h3>
    </div>
    <fieldset>
      <div class="text">
        <label for="title">Name</label>
        <input type="text" id="title" name="title" />
      </div>
      <div class="text">
        <label for="category">Category</label>
        <input type="text" id="category" name="category" />
      </div>
      <div class="text">
        <label for="version">Version</label>
        <input type="text" id="version" name="version" />
      </div>
      <div class="actions">
        <button type="submit">Search</button>
      </div>
    </fieldset>
    <fieldset class="includes">
      <h4><a id="toggle-advanced" class="btn" href="#advanced"><span>&#x2712;</span> Advanced</a></h4>
      <div id="advanced" class="js-hide">
        <h5>Matching</h5>
        <div class="radio">
          <input type="radio" checked="checked" value="" id="match_anywhere" name="match" />
          <label for="match_anywhere">Anywhere</label>
        </div>
        <div class="radio">
          <input type="radio" value="start" id="match_start" name="match" />
          <label for="match_start">Start</label>
        </div>
        <div class="radio">
          <input type="radio" value="end" id="match_end" name="match" />
          <label for="match_end">End</label>
        </div>
        <div class="radio">
          <input type="radio" value="exact" id="match_exact" name="match" />
          <label for="match_exact">Exact</label>
        </div>
        <h5>Include in Results</h5>
        <div class="checkbox">
          <input type="checkbox" checked="checked" value="added" id="added" name="include[]" />
          <label for="added">Version Added</label>
        </div>
        <div class="checkbox">
          <input type="checkbox" checked="checked" value="params" id="params" name="include[]" />
          <label for="params">Arguments</label>
        </div>
      
        <div class="checkbox">
          <input type="checkbox" checked="checked" value="desc" id="short_description" name="include[]" />
          <label for="short_description">Short Description</label>
        </div>
        <div class="checkbox">
          <input type="checkbox" checked="checked" value="longdesc" id="long_description" name="include[]" />
          <label for="long_description">Long Description</label>
        </div>
      </div>
    </fieldset>
  </form>
<p><a id="clear-results" href="#">Clear Results</a></p>
<div id="log"></div>
</div>
<h4>A Little Less Basic Demo</h4>
<p>I replicated this search form on a test server and added back-button support with <a href="http://benalman.com/projects/jquery-bbq-plugin/">Ben Alman's crazy delicious BBQ Plugin</a>. Try out <a href="http://test.learningjquery.com/jqas/">the demo</a>.</p>
<h4>But wait! There's more!</h4>
<p>I put the whole shebang (well, except for the server-side stuff) in a GitHub repo. <a href="http://github.com/kswedberg/jQuery-API-Search">Clone it, fork it, have your way with it</a>.</p>

<h4>But Wait! There's Less!</h4>
<p>As I mentioned above, this little project is incomplete. If you peek at the scripts, you'll quickly spot some major chunks of code in need of refactoring. Also, had I but world enough and time, I'd use a nice templating system such as <a href="http://github.com/janl/mustache.js">mustache.js</a> or the <a href="http://github.com/nje/jquery-tmpl">jQuery Templates plugin</a> to output the search results. But I'll leave all that for another day (or another developer).  </p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/v2nHIDXFeWI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2010/09/introducing-jquery-api-search/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2010/09/introducing-jquery-api-search</feedburner:origLink></item>
		<item>
		<title>Great Ways to Learn jQuery</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/tqInqtf_IXE/great-ways-to-learn-jquery</link>
		<comments>http://www.LEARNINGJQUERY.COM/2010/07/great-ways-to-learn-jquery#comments</comments>
		<pubDate>Wed, 07 Jul 2010 15:11:38 +0000</pubDate>
		<dc:creator>Marc Grabanski</dc:creator>
				<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://learningjquery.com/?p=1164</guid>
		<description>These jQuery resources will set you on the path towards mastering jQuery. Written Articles Getting Started with jQuery - this is the official jQuery getting started guide. jQuery for JavaScript Programmers - Simon Willison (creator of django) gives you an introduction to jQuery for people who already understand JavaScript. jQuery Crash Course - Nathan Smith [...]</description>
			<content:encoded><![CDATA[<p>These jQuery resources will set you on the path towards mastering jQuery.</p> 
<h4>Written Articles</h4> 
<ul> 
	<li><a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery">Getting Started with jQuery</a> - this is the official jQuery getting started guide.</li> 
	<li><a href="http://simonwillison.net/2007/Aug/15/jquery/">jQuery for JavaScript Programmers</a> - Simon Willison (creator of django) gives you an introduction to jQuery for people who already understand JavaScript.</li>
	<li><a href="http://www.digital-web.com/articles/jquery_crash_course/">jQuery Crash Course</a> - Nathan Smith gives a quick introduction to jQuery on Digital Web Magazine.</li> 
	<li><a href="http://www.west-wind.com/presentations/jquery/">Introduction to jQuery</a> - Rick Strahl, well-known for his work developing with Microsoft technologies, gives his introduction to jQuery with part two covering <a href="http://www.west-wind.com/presentations/jquery/jQueryPart2.aspx">using jQuery with ASP.NET.</a></li>
</ul> 
<h4>E-Books</h4> 
<ul> 
	<li><a href="http://jqfundamentals.com/book/book.html">jQuery Fundamentals</a> - open-source e-book written by Rebecca Murphey in collaboration with other well-known members of the jQuery community.</li> 
	<li><a href="http://jqueryenlightenment.com/">jQuery Enlightenment</a> - Cody Lindley's e-book covers advanced topics on jQuery with links to working code examples in jsbin.</li> 
</ul> <span id="more-1164"></span>
<h4>jQuery Books</h4> 
<ul> 
  <li><a href="http://oreilly.com/catalog/9780596159788">jQuery Cookbook</a> - collaborative book by many jQuery team members with recipes on many jQuery topics.</li>
  <li><a href="https://www.packtpub.com/learning-jquery-1.3/book?mid=220409c024ep">Learning jQuery 1.3</a> - walkthrough of learning the jQuery JavaScript library through hands-on examples and explanations. Authored by Karl Swedberg &amp; Jonathan Chaffer</li> 
  <li><a href="https://www.packtpub.com/jquery-1-4-reference-guide/book">jQuery Reference 1.4</a> - practical examples of the entire jQuery 1.4 API. Authored by Karl Swedberg &amp; Jonathan Chaffer</li>
	<li><a href="http://www.manning.com/bibeault2/">jQuery in Action 2nd edition</a> - a fast-paced introduction to jQuery that will take your JavaScript programming to the next level. Authored by Bear Bibeault and Yehuda Katz.</li> 
	<li><a href="https://www.packtpub.com/user-interface-library-for-jquery-ui-1-7/book">jQuery UI 1.7</a> - Build highly interactive web applications with ready-to-use widgets from the jQuery User Interface library. Authored by Dan Wellman.</li> 
</ul> 
<h4>Slideshows</h4> 
<ul> 
	<li><div>John Resig</div>
<div><a href="http://ejohn.org/apps/workshop/intro/">Introduction to jQuery</a></div>
<div><a href="http://ejohn.org/apps/workshop/adv-talk/">Things You Might Not Know About jQuery</a></div>
<a href="http://ejohn.org/apps/workshop/adv-talk/index2.html">Improve Your Web App with jQuery</a></li>
	<li><div>Remy Sharp</div>
<div><a href="http://remysharp.com/downloads/j4d-2010.pdf" title="PDF">(PDF download) jQuery for Designers</a></div>
<div><a href="http://remysharp.com/downloads/jquery-2010.pdf" title="PDF">(PDF download) jQuery write a bit more, do lots more</a></div>
<a href="http://remysharp.com/talks/">More talks from Remy</a></li>
	<li><div>Marc Grabanski</div>
	  <div style="width:610px" id="__ss_1254680"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/1Marc/jquery-essentials" title="jQuery Essentials">jQuery Essentials</a></strong><object id="__sse1254680" width="610" height="500"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-essentials-090406094627-phpapp01&#038;stripped_title=jquery-essentials" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse1254680" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-essentials-090406094627-phpapp01&#038;stripped_title=jquery-essentials" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="610" height="500"></embed></object></div>
</li> 
	<li><div>Simon Willison</div>
<div style="width:610px" id="__ss_88304"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/simon/jquery-in-15-minutes">jQuery in 15 minutes</a></strong><object id="__sse1254680" width="610" height="500"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-in-15-minutes1421&#038;stripped_title=jquery-in-15-minutes" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse88304" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-in-15-minutes1421&#038;stripped_title=jquery-in-15-minutes" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="610" height="500"></embed></object></div>
</li></ul> 
<h4>Learning Screencasts</h4> 
<ul> 
	<li><a href="http://net.tutsplus.com/articles/web-roundups/jquery-for-absolute-beginners-video-series/">jQuery for Absolute Beginners</a> - Net Tuts screencast series starts by downloading the library and eventually work our way up to creating an AJAX style-switcher.</li> 
	<li><a href="http://jqueryfordesigners.com/">jQuery for Designers Screencasts</a> - a myriad of screencast videos to teach you jQuery from a designer's perspective.</li> 
	<li><div><a href="http://css-tricks.com/video-screencasts/20-introduction-to-jquery/">Intro to jQuery</a> - Chris Coyier from CSS Tricks gives you an introduction to jQuery.</div>
  <a href="http://css-tricks.com/video-screencasts/35-intro-to-jquery-2/">Intro to jQuery Part 2</a></li> 
  <li><a href="http://store.lullabot.com/products/introduction-to-jquery">Lullbot Introduction to jQuery</a> - this video walks the viewer through the essential concepts and syntaxes needed to start writing simple-yet-powerful JavaScript using jQuery.</li>
  <li><a href="http://marcgrabanski.com/articles/the-jquery-course-prerelease">The jQuery Course Prerelease</a> - Marc Grabanski's project to document all he's learned from four years of jQuery programming into one educational course.</li> 
	<li><a href="http://peepcode.com/products/jquery">Meet jQuery</a> - this Peepcode Screencast teaches you jQuery through building a simple time-tracking application.</li> 
	<li><a href="http://www.bennadel.com/blog/1492-An-Intensive-Exploration-Of-jQuery-With-Ben-Nadel-Video-Presentation-.htm">An Intensive Introduction to jQuery</a> - Ben Nadal takes a deep dive into jQuery.</li> 
</ul> 
<h4>Attend a training course</h4> 
<ul> 
  <li><a href="http://ideafoundry.info/jquery">Idea Foundry jQuery Training</a> - The brains behind <a href="http://collectiveidea.com/">Collective Idea</a> host jQuery classes led by Karl Swedberg in Holland, Michigan.</li>
	<li><a href="http://www.jquerybootcamp.com/">jQuery Boot Camp</a> - Ben Nadal's training course in New York City.</li> 
	<li><a href="https://training.thoughtbot.com/">Thoughtbot Training</a> - look here for training on jQuery from time to time.</li>
</ul> 
<h4>Hire a Corporate jQuery Trainer</h4> 
<ul> 
	<li><a href="http://appendto.com/training">appendTo Training</a> - the jQuery company offers corporate training.</li> 
	<li><a href="http://www.rebeccamurphey.com/">Rebecca Murphey</a> - started the open-source e-book jQuery Fundementals and is a member of the yayQuery podcast.</li> 
	<li><a href="http://www.learningjquery.com/contact/">Karl Swedberg</a> - wrote the Learning jQuery books and runs this website.</li>
	<li><a href="http://marcgrabanski.com">Marc Grabanski</a> - creator of the jQuery UI Datepicker project and blogger.</li>
	<li><a href="http://orderedlist.com/hire-us/">Ordered List</a> - all around great guys, creators of the Harmony CMS, and active speakers and writers.</li> 
</ul> 
<h4>Conferences</h4> 
<ul> 
	<li><a href="http://carsonified.com/online-conferences/jquery/">Carsonified Online jQuery Conference</a></li> 
	<li><a href="http://events.jquery.org">Official jQuery Conference</a> - hosted in Boston, San Francisco, and other locations in the future.</li> 
	<li><a href="http://www.environmentsforhumans.com/jquery_summit/">jQuery Summit</a> - online conference.</li> 
</ul> 
<h4>jQuery Meetups</h4> 
<p>Another great way to learn more jQuery is to start or join a <a href="http://meetups.jquery.com">jQuery Meetup</a>. Check to see if there is a meetup near you, and if not, start one!</p> 
<h4>Podcasts</h4>
<ul>
  <li><a href="http://blog.jquery.com/">Official jQuery Pocast</a></li>
  <li><a href="http://yayquery.com/">yayQuery video podcast</a></li>
</ul>
<h4>More Resources</h4> 
<ul> 
	<li><a href="http://twitter.com/jquery">jQuery on Twitter</a></li>
	<li><a href="http://wefollow.com/tag/jquery">Twitter Directory for Tag jQuery</a></li>
	<li><a href="http://jquerylist.com">jQuery List</a></li>
	<li><a href="http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/">10 Things I Learned from the jQuery Source</a></li> 
</ul>

<h4>Other Blogs</h4> 
<ul> 
	<li><a href="http://remysharp.com/tag/jquery/">Remy Sharp jQuery</a></li>
	<li><a href="http://www.filamentgroup.com/lab">Filament Group</a></li> 
	<li><a href="http://blog.nemikor.com/">Nemikor</a> - Scott González</li>
	<li><a href="http://benalman.com/">Ben Alman</a></li>
	<li><a href="http://paulirish.com">Paul Irish</a></li>
	<li><a href="http://marcgrabanski.com">Marc Grabanski</a></li>
	<li><a href="http://alexsexton.com/">Alex Sexton</a></li>
	<li><a href="http://james.padolsey.com/">James Padolsey</a></li>
	<li><a href="http://msdn.microsoft.com/en-us/scriptjunkie/">Script Junkie</a></li>
	<li><a href="http://www.nczonline.net/">NCZOnline</a> - Nicholas Zakas</li>
	<li><a href="http://perfectionkills.com/">Perfection Kills</a> - Juriy Zaytsev (kangax)</li>
	<li><a href="http://www.phpied.com/">Stoyan Stefanov</a></li>
	<li><a href="http://stevesouders.com/">Steve Souders</a></li>
	<li><a href="http://css-tricks.com/">CSS Tricks</a></li>
	<li><a href="http://www.webdesignerwall.com/">Web Designer Wall</a></li>
	<li><a href="http://www.jankoatwarpspeed.com/">Janko At Warp Speed</a></li> 
	<li><a href="http://www.marcofolio.net/">Marco Folio</a></li>
</ul><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/tqInqtf_IXE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2010/07/great-ways-to-learn-jquery/feed</wfw:commentRss>
		<slash:comments>39</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2010/07/great-ways-to-learn-jquery</feedburner:origLink></item>
		<item>
		<title>Autocomplete Migration Guide</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/SuI9wloA5lo/autocomplete-migration-guide</link>
		<comments>http://www.LEARNINGJQUERY.COM/2010/06/autocomplete-migration-guide#comments</comments>
		<pubDate>Wed, 23 Jun 2010 13:28:54 +0000</pubDate>
		<dc:creator>Jörn Zaefferer</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery UI]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1106</guid>
		<description>The jQuery Autocomplete plugin got a successor recently, the jQuery UI Autocomplete. In this guide we'll look at the old plugin API step-by-step, and how to migrate to the new API. At first it may look like the new plugin supports barely any of the old options. We'll see how all the old options can [...]</description>
			<content:encoded><![CDATA[<p>The <a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jQuery Autocomplete plugin</a> got a successor recently, the <a href="http://jqueryui.com/demos/autocomplete/">jQuery UI Autocomplete</a>. In this guide we'll look at the <a href="http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions">old plugin API</a> step-by-step, and how to migrate to the new API.</p>

<p>At first it may look like the new plugin supports barely any of the old options. We'll see how all the old options can be implemented using the three new options and the six events.</p>

<p>The old plugin had two arguments: data or url, and options. Lets start with that data-or-url argument. With the new autocomplete plugin, you'll just pass the data or url to the <code>source</code> option.</p>

<p>So, with the old plugin you'd have this code:</p>

<div class="igBar"><span id="ljavascript-63"><a href="#" onclick="javascript:showPlainTxt('javascript-63'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-63">
<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;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>And that becomes, easy enough:</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><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</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; source<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#93;</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>The same applies if you provided a URL as the first argument, although there is a difference between the two plugins for remote data. The old plugin expected a special format with pipes to separate values and newlines to separate rows. That is gone for good, the Autocomplete widget now works with JSON by default. The simplest form is the same as in the example above, an array of string values.</p>

<p>Instead of an array of strings, the widget also accepts an array of objects, with at least a label or value property, or both, in addition to whatever else you need. More on that can be found in the documentation and various demos, eg. <a href="http://jqueryui.com/demos/autocomplete/#custom-data">the Custom Data demo</a> shows how to use custom properties and even display them.</p>

<p>Lets look through the rest of the options for the old plugin, and what to do with them for the new plugin:</p>
<ul>
    <li><strong>autoFill</strong>: Gone with no immediate replacement available, for good reasons: The default behaviour when selecting items via keyboard now puts the focussed value into the input, like the Firefox Awesomebar does it. It's not the same as what the autoFill option did, but there should be no need to recreate that effect.</li>
    <li><strong>cacheLength</strong>: There is no built-in caching support anymore, but it's really easy to implement your own, as shown by the <a href="http://jqueryui.com/demos/autocomplete/#remote-with-cache">Remote with caching demo</a>.</li>
    <li><strong>delay</strong>: Still exists with the same behaviour, but the default is always 300 milliseconds.</li>
    <li><strong>extraParams</strong>: Extra params and all other Ajax related options can be customized by using a callback for the <code>source</code> option. Use <code>$.ajax</code> to send the actual request, with the <code>response</code> callback argument passed to <code>source</code> as the <code>success</code> callback for the <code>$.ajax</code> call. The <a href="http://jqueryui.com/demos/autocomplete/#remote-jsonp">Remote JSONP datasource demo</a> has an example for that.</li>
    <li><strong>formatItem, formatMatch, formatResult, highlight</strong>: All gone, instead use the <code>source</code> option to either provide the formatted data from your serverside, or implement a custom source to do special formatting. The <a href="http://jqueryui.com/demos/autocomplete/#combobox">combobox demo</a> shows how to do that, with a more <a href="http://www.learningjquery.com/2010/06/a-jquery-ui-combobox-under-the-hood">extensive explanation of that demo right on this site</a>.</li>
    <li><strong>matchCase, matchContains, matchSubset</strong>: All gone, too. The builtin matcher for local data will do a case-insensitive match-contains, everything else has to be implemented on the serverside or using the source option. The combobox linked just above also has an example for that.</li>
    <li><strong>max</strong>: Gone; if your server sends too many items, pass a function for the <code>source</code> option that calls <code>$.ajax</code> and truncates or filters the resulting list.</li>
    <li><strong>minChars</strong>: Still present, but was renamed to <code>minLength</code>. Behaves just the same, even the default is still the same, with <code>minLength: 1</code>.</li>
    <li><strong>multiple, multipleSeperator</strong>: Not built-in anymore, but easy to recreate. There are two demos for this, <a href="http://jqueryui.com/demos/autocomplete/#multiple">once with local data</a>, <a href="http://jqueryui.com/demos/autocomplete/#multiple-remote">once with remote data</a>.</li>
    <li><strong>mustMatch</strong>: Gone, but easy to implement with the <code>select</code> event. Once more, the <a href="http://jqueryui.com/demos/autocomplete/#combobox">combobox provides an example</a> for that.</li>
    <li><strong>scroll, scrollHeight</strong>: These option are gone, but the underlying Menu widget actually has support for scrolling. If you have enough items and specify a height via CSS, the menu will scroll.</li>
    <li><strong>selectFirst</strong>: Similar to autoFill (at the top of this list), this option is gone and has now immediate replacement, nor a need for one. The behaviour for selecting values is solid enough to make this option redundant.</li>
    <li><strong>width</strong>: Gone and not required anymore. The menu will automatically be as wide as the input it completes, or wider, as the content requires. And you can always restrict with width using CSS.</li>
</ul>

<p>And thats about it. If you're still looking for a particular replacement, take a look at the various events available, and study the use of the source-option within the various demos. If you still have a question, post on the <a href="http://forum.jquery.com/using-jquery-ui">Using jQuery UI forum</a>. If you spot some mistake or see something that can be improved in this article, please let us know in the comments.</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/SuI9wloA5lo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2010/06/autocomplete-migration-guide/feed</wfw:commentRss>
		<slash:comments>32</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2010/06/autocomplete-migration-guide</feedburner:origLink></item>
		<item>
		<title>A jQuery UI Combobox: Under the hood</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/hSX4baOTkUg/a-jquery-ui-combobox-under-the-hood</link>
		<comments>http://www.LEARNINGJQUERY.COM/2010/06/a-jquery-ui-combobox-under-the-hood#comments</comments>
		<pubDate>Tue, 08 Jun 2010 10:00:15 +0000</pubDate>
		<dc:creator>Jörn Zaefferer</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery UI]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1055</guid>
		<description>Update on 2010-08-17: This article was updated to reflect the changes to the combobox code in jQuery UI 1.8.4 The jQuery UI 1.8 release brings along the new autocomplete widget. An autocomplete adds a list of suggestions to an input field, displayed and filtered while the user is typing. This could be attached to a [...]</description>
			<content:encoded><![CDATA[<strong>Update on 2010-08-17: This article was updated to reflect the changes to the combobox code in <a href="http://blog.jqueryui.com/2010/08/jquery-ui-1-8-4/">jQuery UI 1.8.4</a></strong>

<p>The <a href="http://blog.jqueryui.com/2010/03/jquery-ui-18/">jQuery UI 1.8 release</a> brings along the new <a href="http://jqueryui.com/demos/autocomplete/">autocomplete widget</a>. An autocomplete adds a list of suggestions to an input field, displayed and filtered while the user is typing. This could be attached to a search field, suggesting either search terms or just matching results for faster navigation. But what if there is a fixed list of options, usually implemented as a standard HTML <code>select</code> element, where the ability to filter would help users find the right value way faster?</p>

<p>That's a "combobox." A combobox works like a select, but also has an input field to filter the options by typing. jQuery UI 1.8 actually provides a sample implementation of a <a href="http://jqueryui.com/demos/autocomplete/#combobox">combobox as a demo</a>. In this article, we'll look under the hood of the combobox demo, to explore both the combobox widget and the autocomplete widget that it uses.</p>

<p>Let's starts with the initial markup:</p>
<span id="more-1055"></span>
<div class="igBar"><span id="lhtml-75"><a href="#" onclick="javascript:showPlainTxt('html-75'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-75">
<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;label&gt;</span></span>Your preferred programming language: <span style="color: #009900;"><span style="color: #000000;">&lt;/label&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;select&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #000000;">&gt;</span></span>asp<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #000000;">&gt;</span></span>c<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cpp&quot;</span><span style="color: #000000;">&gt;</span></span>c++<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cf&quot;</span><span style="color: #000000;">&gt;</span></span>coldfusion<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;g&quot;</span><span style="color: #000000;">&gt;</span></span>groovy<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;h&quot;</span><span style="color: #000000;">&gt;</span></span>haskell<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;j&quot;</span><span style="color: #000000;">&gt;</span></span>java<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js&quot;</span><span style="color: #000000;">&gt;</span></span>javascript<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;p1&quot;</span><span style="color: #000000;">&gt;</span></span>perl<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;p2&quot;</span><span style="color: #000000;">&gt;</span></span>php<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;p3&quot;</span><span style="color: #000000;">&gt;</span></span>python<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #000000;">&gt;</span></span>ruby<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #000000;">&gt;</span></span>scala<span style="color: #009900;"><span style="color: #000000;">&lt;/option&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;/select&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Nothing special there, just a label and a select element with a few options.</p>

<p>The code to apply the combobox widget to the select is quite simple, too:</p>

<div class="igBar"><span id="ljavascript-76"><a href="#" onclick="javascript:showPlainTxt('javascript-76'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-76">
<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;">&quot;select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">combobox</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Let's look at the code for this combobox widget. First, the full code, to give you an overview. We'll dig into the details step-by-step afterwards.</p>

<div class="igBar"><span id="ljavascript-77"><a href="#" onclick="javascript:showPlainTxt('javascript-77'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-77">
<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;">widget</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui.combobox&quot;</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; _create<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: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</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> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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; selected <span style="color: #339933;">=</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=children"><span style="">children</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;:selected&quot;</span> <span style="color: #009900;">&#41;</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; value <span style="color: #339933;">=</span> selected.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> selected.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</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> input <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;input /&gt;&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a><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="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span> value <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: #660066;">autocomplete</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; &nbsp; &nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=delay"><span style="">delay</span></a><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</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; minLength<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</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; source<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<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; <span style="color: #003366; font-weight: bold;">var</span> matcher <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">autocomplete</span>.<span style="color: #660066;">escapeRegex</span><span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">term</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;i&quot;</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; response<span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=children"><span style="">children</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;option&quot;</span> <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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: #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; <span style="color: #003366; font-weight: bold;">var</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a> <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="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</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; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> &#038;& <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>request.<span style="color: #660066;">term</span> <span style="color: #339933;">||</span> matcher.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">return</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; label<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</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; <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</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; <span style="color: #3366CC;">&quot;(?![^&#038;;]+;)(?!&lt;[^&lt;&gt;]*)(&quot;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">autocomplete</span>.<span style="color: #660066;">escapeRegex</span><span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">term</span><span style="color: #009900;">&#41;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;)(?![^&lt;&gt;]*&gt;)(?![^&#038;;]+;)&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;gi&quot;</span><span style="color: #009900;">&#41;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;&lt;strong&gt;$1&lt;/strong&gt;&quot;</span><span style="color: #009900;">&#41;</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; &nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><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; &nbsp; &nbsp; &nbsp; &nbsp; option<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</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: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</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; <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; &nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event<span style="color: #339933;">,</span> ui <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; ui.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">option</span>.<span style="color: #660066;">selected</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._trigger<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #339933;">,</span> event<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; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">:</span> ui.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">option</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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; <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; &nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=change"><span style="">change</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<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; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>ui.<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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> matcher <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;^&quot;</span> <span style="color: #339933;">+</span> $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">autocomplete</span>.<span style="color: #660066;">escapeRegex</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</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> <span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;i&quot;</span> <span style="color: #009900;">&#41;</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; &nbsp; &nbsp; valid <span style="color: #339933;">=</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=children"><span style="">children</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;option&quot;</span> <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span> matcher <span style="color: #009900;">&#41;</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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selected</span> <span style="color: #339933;">=</span> valid <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</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> <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; &nbsp; &nbsp; &nbsp; &nbsp; &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; &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; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>valid <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; <span style="color: #006600; font-style: italic;">// remove invalid value, as it didn't match anything</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: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span> <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&quot;</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; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&quot;</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: #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; &nbsp; &nbsp; &nbsp; &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; &nbsp; &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; &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; &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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ui-widget ui-widget-content ui-corner-left&quot;</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; input.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;autocomplete&quot;</span> <span style="color: #009900;">&#41;</span>._renderItem <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> ul<span style="color: #339933;">,</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: normal; 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: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;li&gt;&lt;/li&gt;&quot;</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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;item.autocomplete&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;a&gt;&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">label</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/a&gt;&quot;</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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span> ul <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;">&#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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;button&gt;&nbsp;&lt;/button&gt;&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;tabIndex&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</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="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Show All Items&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span> input <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: #660066;">button</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; &nbsp; &nbsp; icons<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; primary<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ui-icon-triangle-1-s&quot;</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: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #339933;">:</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; &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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=removeClass"><span style="">removeClass</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui-corner-all&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui-corner-right ui-button-icon&quot;</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="/wp-content/themes/ljq/docs-1.7.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: #006600; font-style: italic;">// close if already visible</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;widget&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</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; input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</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; <span style="color: #000066; font-weight: bold;">return</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></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;">// pass empty string as value to search for, displaying all results</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</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; input.<span style="color: #000066;">focus</span><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; <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: 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>Let's break this down, piece by piece:</p>

<div class="igBar"><span id="ljavascript-78"><a href="#" onclick="javascript:showPlainTxt('javascript-78'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-78">
<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;">widget</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui.combobox&quot;</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; _create<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: #006600; font-style: italic;">// all the code</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: 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>This defines a new widget, in the <code>ui</code> namespace (don't use this for your own widgets, it's reserved for jQuery UI widgets) and adds the only method, <code>_create</code>. This is the constructor method for jQuery UI widgets, and will be called only once. In versions prior to 1.8 it was called <code>_init</code>. The <code>_init</code> method still exists, but it is called each time you call <code>.combobox()</code> (with or without options). Keep in mind that our widget implementation is not complete, as it lacks the <code>destroy</code> method. It's just a demo.</p>

<p>Coming up next is the creation of an input element and applying the autocomplete to it, with data provided by the <code>select</code> element.</p>

<div class="igBar"><span id="ljavascript-79"><a href="#" onclick="javascript:showPlainTxt('javascript-79'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-79">
<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> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</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> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; selected <span style="color: #339933;">=</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=children"><span style="">children</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;:selected&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; value <span style="color: #339933;">=</span> selected.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> selected.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</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> input <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;input /&gt;&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a><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="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span> value <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: #660066;">autocomplete</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; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=delay"><span style="">delay</span></a><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</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; minLength<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; source<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<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: #006600; font-style: italic;">// implements retrieving and filtering data from the select</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<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: #006600; font-style: italic;">// implements first part of updating the select with the selection</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=change"><span style="">change</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<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: #006600; font-style: italic;">// implements second part of updating the select with the selection</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: 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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ui-widget ui-widget-content ui-corner-left&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>It starts with a few variable declarations: <code>var self = this</code> will be used inside callbacks below, where <code>this</code> will refer to something else. The <code>var select</code> references the <code>select</code> element on which the combobox gets applied. To replace the <code>select</code> with the text <code>input</code>, the <code>select</code> is hidden.</p>

<p>The selected and value variables are used to initialized the autocomplete with the current value of the select.</p>

<p>An <code>input</code> element is created from scratch, inserted after the <code>select</code> element into the DOM, and transformed into an autocomplete widget. All three autocomplete options are customized:</p>
<ul>
  <li><strong>delay</strong> specifies the amount of time to wait for displaying data between each key press, here set to zero as the data is local</li>
  <li><strong>minLength</strong> is set to 0, too, so that a cursor-down or -up key press will display the autocomplete menu, even when nothing was entered.</li>
  <li><strong>source</strong> provides the filtered data to display</li>
</ul>

<p>Let's break down the <code>source</code> implementation:</p>

<div class="igBar"><span id="ljavascript-80"><a href="#" onclick="javascript:showPlainTxt('javascript-80'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-80">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">source<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<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> matcher <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">autocomplete</span>.<span style="color: #660066;">escapeRegex</span><span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">term</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;i&quot;</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; response<span style="color: #009900;">&#40;</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=children"><span style="">children</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;option&quot;</span> <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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: #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> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a> <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="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</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; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> &#038;& <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>request.<span style="color: #660066;">term</span> <span style="color: #339933;">||</span> matcher.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">return</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; label<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;(?![^&#038;;]+;)(?!&lt;[^&lt;&gt;]*)(&quot;</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; &nbsp; &nbsp; $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">autocomplete</span>.<span style="color: #660066;">escapeRegex</span><span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">term</span><span style="color: #009900;">&#41;</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; &nbsp; &nbsp; <span style="color: #3366CC;">&quot;)(?![^&lt;&gt;]*&gt;)(?![^&#038;;]+;)&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;gi&quot;</span><span style="color: #009900;">&#41;</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; &nbsp; <span style="color: #3366CC;">&quot;&lt;strong&gt;$1&lt;/strong&gt;&quot;</span><span style="color: #009900;">&#41;</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; value<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><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; option<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</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>;</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> <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: #339933;">,</span></div></li>
</ol></div>
</div></div><br />

<p>There is a bit of matching and mapping involved here: At first, a regular expression object is defined, based on the entered term, escaped with the help of the <code>$.ui.autocomplte.escapeRegex</code> utility method. This regex gets reused in the function below. The <code>response</code> argument, a callback, gets called, to provide the data to display. The argument passed is the result of the call to <code>select.find("option").map(callback)</code>. That finds all <code>option</code> elements within our original <code>select</code>, then maps each <code>option</code> to a different object, implemented in another callback passed to the <code>map</code> method.</p>

<p>This callback will return <code>undefined</code>, thereby removing an item, when a search term is present and the text of the option doesn't match the entered value. Otherwise (no term, or it matches), it'll return an object with three properties:</p>
<ul>
  <li><strong>label</strong>: based on the text of the option, with the matched term highlighted with some regexing (another example of a write-only regular expression)</li>
  <li><strong>value</strong>: the unmodified text of the option, to be inserted into the text input field</li>
  <li><strong>option</strong>: the option element itself, to update the select (via the selected-property) or to pass on in custom events</li>
</ul>

<p>The <strong>label</strong> and <strong>value</strong> properties are expected by the autocomplete widget, the <strong>option</strong> property has an arbitrary name, used here only by the combobox widget.</p>

<p>Before, I mentioned that the combobox widget customizes all three autocomplete options, but there were actually five options specified. The fourth and fifth properties, <code>select</code> and <code>change</code>, are event. This is the select implementation:</p>

<div class="igBar"><span id="ljavascript-81"><a href="#" onclick="javascript:showPlainTxt('javascript-81'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-81">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<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; ui.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">option</span>.<span style="color: #660066;">selected</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; self._trigger<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;selected&quot;</span><span style="color: #339933;">,</span> event<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; <span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">:</span> ui.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">option</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
</ol></div>
</div></div><br />

<p>The <code>ui.item</code> argument refers to the data we provided in the source option. Via the <code>ui.item.option</code> property we can update the underlying select to the selected item, as well as triggering a selected events for further customization of the combobox widget.</p> 

<p>To cover the case where no selection is made, the change event is used:</p>

<div class="igBar"><span id="ljavascript-82"><a href="#" onclick="javascript:showPlainTxt('javascript-82'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-82">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=change"><span style="">change</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<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;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>ui.<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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> matcher <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;^&quot;</span> <span style="color: #339933;">+</span> $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">autocomplete</span>.<span style="color: #660066;">escapeRegex</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</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> <span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;i&quot;</span> <span style="color: #009900;">&#41;</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; valid <span style="color: #339933;">=</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; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=children"><span style="">children</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;option&quot;</span> <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span> matcher <span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selected</span> <span style="color: #339933;">=</span> valid <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</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: #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; &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; <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; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>valid <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: #006600; font-style: italic;">// remove invalid value, as it didn't match anything</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: #000066; font-weight: bold;">this</span> <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=select"><span style="">select</span></a>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&quot;</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: #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; &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: #009900;">&#125;</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: #339933;">,</span></div></li>
</ol></div>
</div></div><br />

<p>Here the entered value is used to try and match a selection. If the value doesn't match anything, it'll get removed, and the underlying select is set to an empty value, too.</p>

<p>The next block customizes the _renderItem method of the underlying autocomplete. This is necessary to output the highlighting on each row, as autocomplete by default will escape any html.</p>
	
<div class="igBar"><span id="ljavascript-83"><a href="#" onclick="javascript:showPlainTxt('javascript-83'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-83">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">input.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;autocomplete&quot;</span> <span style="color: #009900;">&#41;</span>._renderItem <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> ul<span style="color: #339933;">,</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;li&gt;&lt;/li&gt;&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;item.autocomplete&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</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="/wp-content/themes/ljq/docs-1.7.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;a&gt;&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">label</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/a&gt;&quot;</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="/wp-content/themes/ljq/docs-1.7.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span> ul <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>
</ol></div>
</div></div><br />

<p>And finally, the last block creates the button that opens the full list of options:</p>

<div class="igBar"><span id="ljavascript-84"><a href="#" onclick="javascript:showPlainTxt('javascript-84'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-84">
<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;">&quot;&lt;button&gt;&nbsp;&lt;/button&gt;&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;tabIndex&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;title&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Show All Items&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span> input <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: #660066;">button</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; icons<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; primary<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ui-icon-triangle-1-s&quot;</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: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #339933;">:</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;"><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;">.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=removeClass"><span style="">removeClass</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui-corner-all&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui-corner-right ui-button-icon&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.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; <span style="color: #006600; font-style: italic;">// close if already visible</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;">if</span> <span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;widget&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</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; input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</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: #000066; font-weight: bold;">return</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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// pass empty string as value to search for, displaying all results</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</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; input.<span style="color: #000066;">focus</span><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>Another element is created on-the-fly. It gets <code>tabIndex="-1"</code> to take it out of the tab order, as it's mostly useful for mouse interactions. Keyboard interaction is already covered by the <code>input</code> element. It gets a <code>title</code> attribute to provide a tooltip and is inserted after the <code>input</code> element into the DOM. A call to <code>.button()</code> with some options together with a bit of class-mangling transforms the button into a Button widget that displays a down-arrow icon with rounded corners on the right (the input has rounded-corners on the left).</p>

<p>Finally a <code>click</code> event is bound to the button: If the autocomplete menu is already visible, it gets closed, otherwise the autocomplete's <code>search</code> method is called with an empty string as the argument, to search for all elements, independent of the current value within the input. As the input handles keyboard input, it gets focused. Having focus on the button would be useless or would require duplicate keyboard interaction that the input already supports.</p>

<p>And that's it! We can see that the autocomplete widget is flexible enough to allow all this with option customization, events, and calling a few methods. We don't have to "subclass" autocomplete (creating a new widget with the autocomplete as the parent prototype instead of <code>$.widget</code>). Instead, we can make the combobox mostly independent of any internal or private autocomplete methods. For the full experience and latest version, check out the <a href="http://jqueryui.com/demos/autocomplete/#combobox">combobox demo</a> on the <a href="http://jqueryui.com/">jQuery UI</a> site.</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/hSX4baOTkUg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2010/06/a-jquery-ui-combobox-under-the-hood/feed</wfw:commentRss>
		<slash:comments>57</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2010/06/a-jquery-ui-combobox-under-the-hood</feedburner:origLink></item>
		<item>
		<title>Now you see me…  show/hide performance</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/CTZt9FE20P4/now-you-see-me-showhide-performance</link>
		<comments>http://www.LEARNINGJQUERY.COM/2010/05/now-you-see-me-showhide-performance#comments</comments>
		<pubDate>Tue, 04 May 2010 12:51:18 +0000</pubDate>
		<dc:creator>Josh Powell</dc:creator>
				<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1114</guid>
		<description>I just got back from the jQuery conference in San Francisco. Wow, what an event. In addition to some incredible talks, I had the opportunity to speak with Rey Bango, Johnathon Sharp, and, of course, John Resig. Any conference where you get to talk to some of the most influential people in jQuery is a [...]</description>
			<content:encoded><![CDATA[<p>I just got back from the jQuery conference in San Francisco.  Wow, what an event.  In addition to some incredible talks, I had the opportunity to speak with Rey Bango, Johnathon Sharp, and, of course, John Resig. Any conference where you get to talk to some of the most influential people in jQuery is a win in my book. The "High Performance JQuery" presentation especially caught my attention when the speaker, Robert Duffy, said that <code>.hide()</code> and <code>.show()</code> were slower than changing the css directly. Not having occasion to ask him why, I benchmarked the various ways to hide DOM elements and looked into the jQuery source to find out what is going on.</p>

<p>The HTML I tested against was a page of 100 div tags with a class and some content, I cached the selector <code>$('div')</code> to use with each method to exclude the time needed to find all the div elements on the page from the test. I used jQuery 1.4.2 for the testing, but keep in mind that the algorithms behind the method calls can change dramatically from version to version. What is true for 1.4.2 is not necessarily true for other versions of the library.</p>

<p>The methods I tested were <code>.toggle()</code>, <code>.show()</code> &amp; <code>.hide()</code>, <code>.css({'display':'none'})</code> &amp; <code>.css({'display':'block'})</code>, and <code>.addClass('hide')</code> &amp; <code>.removeClass('hide')</code>. I also tested modifying an attribute of a <code>&lt;style&gt;</code> element.</p>
<span id="more-1114"></span>
<h4>.show() &amp; .hide()</h4>

<p>These were, in fact, comparatively slow methods of hiding DOM elements across all browsers. One of the main reasons is that <code>.hide()</code> has to save the notion of what the display attribute was before, so that .show() can restore it.  It does this using the <code>.data()</code> jQuery method, storing that information on the DOM element.  In order to do so, <code>.hide()</code> loops through every element twice: once to save the current display value, and then once to update the display style to none. According to a comment in the source, this prevents the browser from reflowing with every loop. The <code>.hide()</code> method also checks to see if you pass in a parameter to animate the hiding with an effect. Even passing in a 0 dramatically slows down the performance. Performance was slowest on the first call to <code>.hide()</code>; subsequent calls were faster.
</p>

<pre>Browser      hide/show
FF3.6 -         <em>29ms / 10ms </em>
Safari 4.05 -   <em>6ms / 1ms</em>
Opera 10.10 -   <em>9ms / 1ms</em>
Chrome 5.0.3 -  <em>5ms / 1ms</em>
IE 6.0  -       <em>31ms / 16ms </em>
IE 7.0  -       <em>15ms / 16ms </em>
</pre>

<h4>.toggle()</h4>

<p>This was, by far, the slowest method of hiding all of the div elements. It iterates through every element returned by the selector, checks to see if the element is currently visible, and then calls <code>.hide()</code> on visible elements one at a time and <code>.show()</code> on hidden ones one at a time. It also has to check to see if you are passing in a boolean to force everything to <code>.show()</code> or <code>.hide()</code> and check to see if you are passing in functions to toggle instead of toggling visibility. There seems to be some opportunity for optimization of this function, as one could select all of the hidden elements of the selector and call <code>.show()</code> on them all at once and then select the remaining elements in the selector and call <code>.hide()</code> on them at the same time. If you are so inclined, I encourage you to check out the source and see if you can eke out any performance gains.</p>

<pre>Browser      hide/show
FF3.6 -         <em>80ms / 59ms </em>
Safari 4.05 -   <em>24ms / 30ms </em>
Opera 10.10 -   <em>67ms / 201ms</em>
Chrome 5.0.3 -  <em>55ms / 20ms </em>
IE 6.0  -       <em>296ms / 78ms </em>
IE 7.0  -       <em>328ms / 47ms </em>
</pre>

<h4>.addClass() &amp; .removeClass()</h4>

<p>These are pretty snappy methods of hiding/showing elements of the DOM, twice as fast as <code>.show()</code> &amp; <code>.hide()</code> in Firefox and three times as fast in Safari. The differences in IE 6, IE7, Chrome, and Opera are negligible.  It's also worth noting that with 100 DOM nodes, we're talking a total difference of 18ms in Firefox and 4ms in Safari.  The speed difference will only be relevant for very large selections. Adding and removing a class requires a bit more management on your part, since you have to create the class that has a display of none and then have to keep track of CSS priority to make sure your elements get hidden. The way jQuery adds and removes a class is through string manipulation, so I imagine that as the number of classes on an element grows, this method will get slower, but that is untested speculation on my part.</p>

<pre>Browser      hide/show
FF3.6   -       <em>11ms / 11ms </em>
Safari 4.05 -   <em>2ms / 2ms</em>
Opera 10.10 -   <em>6ms / 3ms</em>
Chrome 5.0.3 -  <em>3ms / 1ms</em>
IE 6.0  -       <em>47ms / 32ms</em>
IE 7.0  -       <em>15ms / 16ms</em>
</pre>

<h4>.css({'display':'none'}) &amp; .css({'display':'block'});</h4>

<p>These methods were very snappy. They showed an improvement over <code>.addClass()</code> and <code>.removeClass()</code> in Opera and IE 6/7 and about the same in other browsers. They work great if you know the current display style of all the elements you are changing, or at least have not changed the display style inline. If you have changed the display style inline, then you will need to make sure you set the correct value when you make the element visible again. If you are just using the elements' default display value or set the display value in the css, then you can just remove the style like so, <code>.css({'display':''})</code>, and it will revert to whatever value it has in the css or by default.  As a library, jQuery can't assume that the display element wasn't set inline, so it has to manually keep track of it. That is the main slowness you can avoid since you know you won't be setting the display inline.</p>

<pre>Browser      hide/show
FF3.6   -       <em>14ms / 12ms</em>
Safari 4.05 -   <em>2ms / 1ms</em>
Opera 10.10 -   <em>2ms / 2ms</em>
Chrome 5.0.3 -  <em>2ms / 1ms</em>
IE 6.0  -       <em>16ms / 16ms</em>
IE 7.0  -       <em>0ms / 0ms</em>  // The <a href="http://ejohn.org/blog/accuracy-of-javascript-time/">usual caveat</a> about inaccuracy of IE clocks applies.
</pre>

<h4>Disabling stylesheets</h4>

<p>For fun, I thought, "What if instead of manipulating every DOM node and changing things, we just futz with the stylesheet?"  Could there be speed improvements there? I mean, the methods benchmarked above are plenty fast for everyday use, but what if I had 10,000 nodes on a page I wanted to show and hide? It would be slow just selecting them all. But, if I could manipulate the stylesheet, I could avoid the entire overhead. Let me just tell you that way is fraught with peril. </p>
<p>There are, of course, cross browser issues when manipulating stylesheets, since jQuery doesn't abstract them away for you. First, I tried to see if I could append a style tag with the css class as a string using jQuery, but got inconsistent results across browsers. Then I tried creating the stylesheet node and class using JavaScript, but there were different APIs and it ended up being too slow to justify. So finally, forgoing an attempt to do this in a programmatic way, I ended up just writing a style tag with a class in the head of the document. It's far too slow to create the stylesheet programmatically, but if it's already there then it is trivial to give it an ID and use its disabled attribute.</p>
<div class="igBar"><span id="lhtml-88"><a href="#" onclick="javascript:showPlainTxt('html-88'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-88">
<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;style</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special_hide&quot;</span><span style="color: #000000;">&gt;</span></span>.special_hide { display: none; }<span style="color: #009900;"><span style="color: #000000;">&lt;/style&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: #808080; font-style: italic;">&lt;!-- &nbsp;... &nbsp;--&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;div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special_hide&quot;</span><span style="color: #000000;">&gt;</span></span>Special hide DIV<span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />
<p>Then in javascript&hellip;</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: #009900;">&#40;</span><span style="color: #3366CC;">'#special_hide'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled, '</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #3366CC;">');</span></div></li>
</ol></div>
</div></div><br />
<p>and BAM, you just displayed all of your elements with a class of &ldquo;special_hide&rdquo;. To hide them all again, just do&hellip;</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;">'#special_hide'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'disabled'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'false'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>and they are now all hidden.  The total javascript processing time was 0-1ms across all browsers.  The only javascript you are doing is changing an attribute.  Of course, there is still the time the browser takes to reflow and repaint the page, but you've virtually eliminated all the javascript processing time. If you call any of the other methods, <code>.toggle()</code>, <code>.hide()</code>, or <code>.css()</code>, this method will stop working on those elements because they set the css style inline, which has higher precedence than other css. To make this method work again, simply do a <code>.css(‘display’, ‘’)</code> to remove the inline style.  This method also requires the most work on your part, because you have to define the class and give the class to all of the elements on the page you want to show/hide at the same time, but if you are dealing with extremely large sets, this might just be worth it.                                                                                                                                                                                                                               </p>

<p>To recap, here is a list of methods to change the display of elements in order from fastest to slowest:</p>

<ol>
  <li>Enabling/Disabling a stylesheet</li>
  <li>.css('display', ''), .css('display', 'none')</li>
  <li>.addClass(), .removeClass()</li>
  <li>.show(), .hide()</li>
  <li>.toggle()</li>
</ol>
<p>Also note that for the majority of use cases, all of these methods are plenty fast to use.  When you start having to manipulate large jQuery collections, <code>.show()</code> and <code>.hide()</code> might become too slow in IE, and you might need to bump up to <code>.addClass()</code> or <code>.removeClass()</code>.  Enabling/disabling of stylesheets would only be necessary in the most extreme cases, but if things are hiding to slowly for you, you might want to give it a try.</p>
<img src="http://feeds.feedburner.com/~r/LearningJquery/~4/CTZt9FE20P4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2010/05/now-you-see-me-showhide-performance/feed</wfw:commentRss>
		<slash:comments>63</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2010/05/now-you-see-me-showhide-performance</feedburner:origLink></item>
		<item>
		<title>Accessible Showing and Hiding</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/1aweEHYsqCc/accessible-showing-and-hiding</link>
		<comments>http://www.LEARNINGJQUERY.COM/2010/04/accessible-showing-and-hiding#comments</comments>
		<pubDate>Fri, 23 Apr 2010 13:42:31 +0000</pubDate>
		<dc:creator>Filament Group</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[progressive enhancement]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1086</guid>
		<description>Editor's Note: When I started this blog nearly three years ago, one of the first things I did was write a series on showing and hiding elements on a page. The posts were very basic, as was my knowledge at the time. At best, they demonstrated an incomplete answer to the question of how to [...]</description>
			<content:encoded><![CDATA[<p class="note-editor"><strong>Editor's Note:</strong> When I started this blog nearly three years ago, one of the first things I did was write a series on showing and hiding elements on a page. The posts were very basic, as was my knowledge at the time. At best, they demonstrated an incomplete answer to the question of how to selectively reveal content based on user interaction. At worst, they encouraged a solution without any regard to accessibility. That's why I was thrilled when I saw an article on the Filament Group blog describing their accessible collapsible content widget. They've graciously accepted my request to re-post that article here, so without any further ado, here it is&hellip;  </p>
<p>Collapsible content areas are frequently presented in web sites and applications as a way to let users to control how content is shown or hidden on the page. Also called collapsibles, spin-downs, toggle panels, twisties, and content disclosures, they're ideal for selectively displaying optional information — like instructional text or additional details, for example — so users can focus on the task at hand and view this content only as needed.</p>

<p>The collapsible content area widget is fairly simple — a couple of HTML elements controlled with minimal CSS and JavaScript — but when we were researching our book, <a href="http://filamentgroup.com/dwpe"><em>Designing with Progressive Enhancement</em></a>, we discovered that many common approaches to creating collapsible content fail to incorporate accessibility features. Happily, there is a way to build collapsible content with progressive enhancement so it delivers an optimal accessible experience for sighted and screen reader users alike.</p>

<p>The code example described in this article is one of the <a href="http://filamentgroup.com/dwpe/#codeexamples">12 fully-accessible, project-ready, progressive enhancement-driven widgets</a> that accompanies our new book, <em><a href="http://filamentgroup.com/dwpe">Designing with Progressive Enhancement</a></em>.</p>
<span id="more-1086"></span>
<h4>View a demo</h4>
<p>Before we dive in to the details, let's take a look at the widget in question. Here's an error dialog we created for a photo site, which appears when a photo upload fails — it provides a high-level summary, and a collapsible Details block with supplemental content about which photos failed to upload and why:</p>

<div><a class="iframe" data-style="height: 20em;" href="http://dwpe.googlecode.com/svn/trunk/collapsible/index.html">Demo page of collapsible content plugin</a></div>

<p class="note"><strong>NOTE on the demo's "View low bandwidth" link: </strong> This demo runs on our <a href="http://enhancejs.googlecode.com">EnhanceJS framework</a>, which adds a "View low-bandwidth version" link to allow users to toggle from a basic to enhanced view, and drops a cookie on change. If you click the link to view the low-bandwidth version of the demo, you'll need to click it again to view the enhanced version of this site on future views. (Learn more about EnhanceJS <a href="/lab/introducing_enhancejs_smarter_safer_apply_progressive_enhancement/">here</a>.)</p>

<h4>So what's the problem here?</h4>
<p>When we first set out to build an accessible collapsible content widget, we thought that the best way to hide the content and keep it accessible to screen readers would be to position it off-screen. We didn't appreciate that while technically this does make the content accessible, positioning content off-screen doesn’t actually provide an accessible <em>experience</em>.</p>

<p>People using assistive technologies like screen readers want to be able to interact with the page just as sighted users do. As accessibility specialist Adam Spencer noted in <a href="http://www.cbc.ca/technology/story/2010/03/31/f-visually-impaired-website-accessibility.html">a recent CBS News story</a>, “True accessibility is giving blind people <em>the same options to access information</em> as sighted ones” (emphasis ours).</p>

<p>While it’s true that hiding collapsible content off-screen at least keeps it in the page and accessible at a baseline level, it’s decidedly sub-par compared with the standard web user’s experience. For example, when a user with a screen reader encounters an accordion widget with ten sections, just like a sighted user they want to hear only the open section, not all content in every section. Screen reader users also expect to use the keyboard to quickly hear the accordion section headings — the screen reader equivalent of quickly scanning the page. When you simply hide content off-screen, these actions aren’t possible; instead, the screen reader reads all content in all the hidden panes, and the screen reader user has no option but to wade through it all in the order it appears in the markup.</p>

<p>Ideally, the experience for both sighted and visually impaired users should be as functionally similar as possible. The W3C Web Content Accessibility Group (WCAG) has outlined <a href="http://www.w3.org/TR/UNDERSTANDING-WCAG20/intro.html#introduction-fourprincs-head">a set of four principles</a> that must be met to ensure that content is accessible. Quickly, they say that it should be:</p>

<ul>
	<li><strong>Perceivable</strong> — Information and user interface components must be presentable to users in ways they can perceive.</li>
	<li><strong>Operable</strong> — User interface components and navigation must be operable.</li>
	<li><strong>Understandable</strong> — Information and the operation of user interface must be understandable.</li>
	<li><strong>Robust</strong> — Content must be robust enough that it can be interpreted reliably by a wide variety of user agents, including assistive technologies.</li>
</ul>

<p>That’s all well and good, but these definitions are a little abstract. What does it really mean?  WCAG provides extended definitions that give us some helpful clues — for example, Perceivable content “can't be invisible to all of their senses”; an Operable interface “cannot require interaction that a user cannot perform”; an Understandable interface’s “content or operation cannot be beyond their understanding”; and it is Robust if “as technologies and user agents evolve, the content should remain accessible”.</p>

<p>In light of these principles, many common techniques for presenting collapsible content fall short. Consider these scenarios:</p>

<ul>
	<li><strong>Hiding content by default in the CSS, and relying on JavaScript events to let the user display it.</strong> If JavaScript is disabled or unavailable, the user has no means of accessing the content, and may not even know it's there. In this case, content is neither perceivable nor operable — or worse, if the design provides a hint (like an open/close icon or "View details" link), the hint <em>is</em> perceivable but the content non-functioning.</li>
	<li><strong>Hiding content by positioning it off the page (e.g., <code>position: absolute; left: -9999px;</code>).</strong> Doing this ensures that the content is available to screen readers. However, it's <em>always</em> available — the user has no ability to control showing or hiding, or manage whether it's is read aloud. As collapsible content it's not operable; and depending on the widget's content, presenting it all simultaneously may not be understandable.</li>
	<li><strong>Providing only a visual indicator, like an icon, on the clickable element to show that it can manipulate visibility of related content.</strong> This works for sighted users, but fails for screen readers. Unless the icon is accompanied by some adequate auditory feedback that content can be shown/hidden, the feature may not be fully perceivable or understandable to the screen reader user.</li>
	<li><strong>Applying JavaScript events to a non-natively-focusable element (such as a heading) to show/hide related content.</strong> While it works for mouse users, this approach does not guarantee that the widget is navigable for keyboard users (particularly in browsers that don't properly support the <code>tabindex</code> attribute), which are necessary for both screen readers and many mobile devices. In other words, it's potentially neither operable nor robust.</li>
</ul>

<p>To create a collapsible content widget that works for everyone — and doesn't compromise the experience for screen reader users — we had to rethink what "accessibility" means when showing and hiding content.</p>

<h4>Our approach</h4>
We start by marking up the page with semantic HTML elements for the heading and content blocks. For example, consider the collapsible widget portion of our error dialog, which consists of a heading element immediately followed by an unordered list:

<div class="igBar"><span id="lhtml-95"><a href="#" onclick="javascript:showPlainTxt('html-95'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-95">
<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;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;message&quot;</span><span style="color: #000000;">&gt;</span></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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">. . .</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;h2&gt;</span></span>Details<span style="color: #009900;"><span style="color: #000000;">&lt;/h2&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>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>purple-flower.tif <span style="color: #009900;"><span style="color: #000000;">&lt;em&gt;</span></span>Not a supported file format<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>em&gt;&lt;<span style="color: #66cc66;">/</span>li&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>flower-photos.zip <span style="color: #009900;"><span style="color: #000000;">&lt;em&gt;</span></span>Not a supported file format<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>em&gt;&lt;<span style="color: #66cc66;">/</span>li&gt;</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>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>This markup provides a usable and natively accessible basic experience in all browsers.</p>

<p>If JavaScript is present, we use it to apply enhancements that transform this markup into a functioning collapsible widget with a number of accessibility features. The enhancement script appends several attributes and elements to the basic markup:</p>
<ul>
	<li>classes are assigned to the heading element which hide the Details content and apply a visual cue (icon) to indicate that it can be expanded or collapsed</li>
	<li>a <code>span</code> tag is appended to the heading immediately before the text label  which contains the word "Show" followed by a single space if the content is hidden by default; when the content is shown, the script dynamically updates this word to "Hide."  This span is intended only for screen readers to audibly describe the heading's function as a toggle link (i.e., "Show Details"), so it's hidden from standard browsers with absolute positioning and a large negative left value</li>
	<li>a standard anchor link element is wrapped around the heading content to allow it to receive keyboard focus and be accessed with the Tab key</li>
	<li>an <code>aria-hidden</code> attribute is assigned to the unordered list to ensure that it is truly hidden from ARIA-enabled screen readers when it is hidden from sight (<code>aria-hidden="true"</code>). While <code>display: none;</code> will sufficiently hide the content in current screen readers, future screen readers may not continue to obey visual styles like <code>display</code>, so adding this attribute is considered good, fail-safe practice.</li>
	<li>classes are also appended to the unordered list to show or hide it visually with CSS. To hide the content, we use the display: none CSS property so that the content is completely hidden from all users</li>
</ul>

<p>The resulting enhanced markup looks like this:</p>

<div class="igBar"><span id="lhtml-96"><a href="#" onclick="javascript:showPlainTxt('html-96'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-96">
<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;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;message&quot;</span><span style="color: #000000;">&gt;</span></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: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">. . .</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;h2</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;collapsible-heading collapsible-heading-collapsed&quot;</span>&gt;&lt;a <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;collapsible-heading-toggle&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000;">&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;span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;collapsible-heading-status&quot;</span><span style="color: #000000;">&gt;</span></span>Show <span style="color: #009900;"><span style="color: #000000;">&lt;/span&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">Details<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>a&gt;&lt;<span style="color: #66cc66;">/</span>h2&gt;</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</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;collapsible-content collapsible-content-collapsed&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>purple-flower.tif <span style="color: #009900;"><span style="color: #000000;">&lt;em&gt;</span></span>Not a supported file format<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>em&gt;&lt;<span style="color: #66cc66;">/</span>li&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>flower-photos.zip <span style="color: #009900;"><span style="color: #000000;">&lt;em&gt;</span></span>Not a supported file format<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>em&gt;&lt;<span style="color: #66cc66;">/</span>li&gt;</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>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>And the enhanced classes for providing feedback are structured as follows – notice that we use a single background image sprite (icon-triangle.png), and simply adjust the background position to show the appropriate state:</p>

<div class="igBar"><span id="lcss-97"><a href="#" onclick="javascript:showPlainTxt('css-97'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">CSS:</span><br /><div id="css-97">
<div class="css" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">. . .</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.collapsible-heading </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">padding-left</span><span style="color: #66cc66;">:</span>15px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">background</span><span style="color: #66cc66;">:</span><span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span>../images/icon-triangle.png<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;color:#800000;">0</span> 6px <span style="color: #993333;">no-repeat</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.collapsible-heading-collapsed </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">background-position</span><span style="color: #3333ff;">:<span style="color: #cc66cc;color:#800000;">0</span> </span>-84px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.collapsible-heading-toggle </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">text-decoration</span><span style="color: #66cc66;">:</span><span style="color: #993333;">none</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">color</span><span style="color: #66cc66;">:</span>#<span style="color: #cc66cc;color:#800000;">333</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.collapsible-heading-status </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">position</span><span style="color: #66cc66;">:</span><span style="color: #993333;">absolute</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">left</span><span style="color: #66cc66;">:</span>-99999px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.collapsible-content-collapsed </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #000000;">display</span><span style="color: #66cc66;">:</span><span style="color: #993333;">none</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">. . .</div></li>
</ol></div>
</div></div><br />
<p class="note"><strong>NOTE on testing browser capabilities: </strong> To ensure that the browser can fully support all enhancements, we recommend using our <a href="http://enhancejs.googlecode.com">EnhanceJS framework</a>, which tests browsers' CSS and JavaScript capabilities and applies enhancements only after those tests are passed. Read more about EnhanceJS here:  <a href="/lab/introducing_enhancejs_smarter_safer_apply_progressive_enhancement/">Introducing EnhanceJS: A smarter, safer way to apply progressive enhancement</a>.</p>

<h4>What we accomplish</h4>
<p>When built as described above, our collapsible content widget incorporates specific features to meet the four accessibility principles outlined by WCAG:</p>
<ul>
	<li>To ensure that it’s <strong>perceivable</strong> and <strong>understandable</strong>, it includes cues for both visual and screen reader users that indicate the heading element will show and hide associated content. Visual users see an icon that changes orientation based on the content's state; for screen reader users, the script conditionally appends the word "Show" or "Hide" before the heading text to describe the content's state and provide appropriate language to indicate how the heading is used. Additionally, the widget provides feedback to ARIA-enabled screen readers with the ARIA attribute, <code>aria-hidden</code>; it's set to true when the content is hidden, and false when shown.</li>
	<li>To make it <strong>operable</strong>, we ensure that the widget is fully navigable and controllable with the keyboard. When CSS and JavaScript enhancements are applied to the page, the script wraps each <code>heading</code> element's text with an anchor element so that the user can navigate to and focus on the heading with the Tab key, and hide or show related content with the Enter key.</li>
	<li>And our progressive enhancement approach helps us ensure it’s <strong>robust</strong>: The foundation is semantic HTML, which is accessible and usable on all browsers; only when a browser is capable of enhancements is the markup transformed into a collapsible widget with enhanced styling and behavior.</li>
</ul>
<h4>Looking ahead</h4>
<p>The HTML5 spec includes a new element called <code>details</code>, which if implemented as proposed, will provide a native collapsible widget that will require no JavaScript and will be accessible without requiring any additional work. No browsers have adopted the <code>details</code> element at this time, but it does present a potential alternative to this approach that we may be able to use in the future. Bruce Lawson provides an <a href="http://www.brucelawson.co.uk/2010/html5-details-element-built-in-and-bolt-on-accessibility/">interesting summary of <code>details</code></a> and an argument in favor of using semantic interactive elements over JavaScript.</p>

<h4>How to use the plugin</h4>
<p>Download the collapsible content plugin script (see instructions below), and reference it and the <a href="http://jquery.com/">jQuery library</a> in your page. You'll also need to append the CSS classes listed above for the collapsed state of the widget. NOTE:  You'll likely need to edit this CSS to fit with your own project's design.</p>

<p>Call the collapsible plugin on the heading or other element that you're using to show related content; the plugin then assumes that the next element in the source order is the content block to show/hide. For example, on DOM ready you can call the plugin on all H2 elements:</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: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.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;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">collapsible</span><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 />
<h4>Where to download it</h4>
<p>If you've already purchased <em>Designing with Progressive Enhancement</em>, you can download all twelve widgets at the <a href="http://filamentgroup.com/dwpe#codeexamples">code examples download page</a>.</p>

<p>For the collapsible content plugin and all others that have been released publicly as open source, <a href="http://dwpe.googlecode.com/files/dwpe-code-public-latest.zip">download the zip</a>.</p>

<p>To learn more, check out <em>Designing with Progressive Enhancement</em>'s open-source plugin Google Code repository: <a href="http://code.google.com/p/dwpe/">http://code.google.com/p/dwpe/</a>.</p>

<h4>Help us improve the code</h4>
<p>We keep track of issues (feature requests or found defects) in the DWPE Google Code site. Please take a look at our list, and feel free to add to it if you find a bug or have a specific idea about how to improve the plugin that we haven't identified: <a title="Issues list for the Collapsible plugin from Designing with Progressive Enhancement" href="http://code.google.com/p/dwpe/issues/list?q=label:Collapsible">http://code.google.com/p/dwpe/issues/list</a>. If you think you can help on a particular issue, please attach a patch and we'll review it as soon as possible.</p>

<h4>Thoughts? Feedback?</h4>
<p>We'd love to hear what you think of this plugin. Please <a href="http://www.filamentgroup.com/lab/expand_and_collapse_content_accessibly_with_progressive_enhancement_jquery/#comments">leave us a comment on our blog</a>!</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/1aweEHYsqCc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.LEARNINGJQUERY.COM/2010/04/accessible-showing-and-hiding/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.LEARNINGJQUERY.COM/2010/04/accessible-showing-and-hiding</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 5/7 queries in 0.009 seconds using disk: basic
Object Caching 572/572 objects using disk: basic
Content Delivery Network via learningjquery.kswedberg.netdna-cdn.com

Served from: www.learningjquery.com @ 2012-05-13 08:06:05 -->

