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

<channel>
	<title>Learning jQuery &#187; jQuery Resources</title>
	<atom:link href="http://www.learningjquery.com/category/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.com</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Tue, 17 Jan 2012 14:14:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery.map() in 1.6</title>
		<link>http://www.learningjquery.com/2011/05/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><![CDATA[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-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: #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-13"><a href="#" onclick="javascript:showPlainTxt('javascript-13'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-13">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> 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-14"><a href="#" onclick="javascript:showPlainTxt('javascript-14'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-14">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> 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-15"><a href="#" onclick="javascript:showPlainTxt('javascript-15'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-15">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">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-16"><a href="#" onclick="javascript:showPlainTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-16">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">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-17"><a href="#" onclick="javascript:showPlainTxt('javascript-17'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-17">
<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-18"><a href="#" onclick="javascript:showPlainTxt('javascript-18'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-18">
<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-19"><a href="#" onclick="javascript:showPlainTxt('javascript-19'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-19">
<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-20"><a href="#" onclick="javascript:showPlainTxt('javascript-20'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-20">
<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-21"><a href="#" onclick="javascript:showPlainTxt('javascript-21'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-21">
<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-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: #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>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2011/05/jquery-map-in-16/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Introducing jQuery API Search</title>
		<link>http://www.learningjquery.com/2010/09/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><![CDATA[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-27"><a href="#" onclick="javascript:showPlainTxt('javascript-27'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-27">
<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-28"><a href="#" onclick="javascript:showPlainTxt('javascript-28'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-28">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><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-29"><a href="#" onclick="javascript:showPlainTxt('javascript-29'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-29">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><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-30"><a href="#" onclick="javascript:showPlainTxt('javascript-30'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-30">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #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>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/09/introducing-jquery-api-search/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Great Ways to Learn jQuery</title>
		<link>http://www.learningjquery.com/2010/07/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><![CDATA[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>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/07/great-ways-to-learn-jquery/feed</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Spring 2010 jQuery Conference: San Francisco Bay Area</title>
		<link>http://www.learningjquery.com/2010/03/spring-2010-jquery-conference-san-francisco-bay-area</link>
		<comments>http://www.learningjquery.com/2010/03/spring-2010-jquery-conference-san-francisco-bay-area#comments</comments>
		<pubDate>Mon, 29 Mar 2010 13:26:17 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Bay Area]]></category>
		<category><![CDATA[California]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[San Francisco]]></category>
		<category><![CDATA[West Coast]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1057</guid>
		<description><![CDATA[Since the fall of 2007, jQuery developers around the world have been making an annual pilgrimage to Boston, Massachusetts, to meet jQuery team members, JavaScript luminaries, and other jQuery developers at the official jQuery Conference. This spring, in response to growing demand, the jQuery Project is offering its first-ever official jQuery conference on the U.S [...]]]></description>
			<content:encoded><![CDATA[<p>Since the fall of 2007, jQuery developers around the world have been making an annual pilgrimage to Boston, Massachusetts, to meet jQuery team members, JavaScript luminaries, and other jQuery developers at the official jQuery Conference. This spring, in response to growing demand, the jQuery Project is offering its first-ever <a href="http://events.jquery.org/2010/sf-bay-area/">official jQuery conference on the U.S west coast</a>. The conference will take place April 24&ndash;25 in Mountain View, California, and will include <a href="http://events.jquery.org/2010/sf-bay-area/speakers/">top-notch presentations</a> from the jQuery team, as well as other web development and performance experts. Additionally, the conference will be preceded by a one-day intensive jQuery training course in downtown San Francisco, led by <a href="http://appendto.com/">appendTo</a> a leading jQuery training and consulting company.</p>
<span id="more-1057"></span>
<p>Priced at $199, the two-day conference is one of the best bargains you'll find anywhere for a full-on tech conference. The jQuery team is committed to keeping the price as low as possible, relying on the <a href="http://jquery.org/sponsors">support</a> of <a href="http://events.jquery.org/2010/sf-bay-area/sponsors/">sponsors</a> to help defray the cost of registration. jQuery developers from beginner to advanced will learn a ton, have a great time, and meet many others who are using jQuery in innovative ways to make the web a better place.</p>
<p>On a personal note, this will be the first jQuery conference ever that I won't be able to attend (regrettably, a scheduling conflict is keeping me home this time). But having heard many of the speakers in the past and having been involved a bit in the planning of this conference, I can assure you that <em>you</em> won't want to miss it. While we hope to continue offering a west-coast event on an annual basis, as we have done with the east-coast event a lot will depend on the level of interest and attendance this time around. So, what are you waiting for? <a href="http://events.jquery.org/2010/sf-bay-area/register/">Sign up now</a>!</p>
<p>One brief personal plug: I will be teaching a <a href="http://ideafoundry.info/jquery">3-day jQuery class</a> June 2&ndash;4 in beautiful Holland, Michigan. If for some reason you can't make it to the conference in April&mdash;or even if you can&mdash;you might want to consider taking the class. This will be the fourth class I've taught with <a href="http://ideafoundry.info/">Idea Foundry</a>, and if it's anything like the others, we'll have a blast, with lots and lots of great learning.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/03/spring-2010-jquery-conference-san-francisco-bay-area/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using setTimeout to Delay Showing Event-Delegation Tooltips</title>
		<link>http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips</link>
		<comments>http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips#comments</comments>
		<pubDate>Wed, 16 Dec 2009 17:47:22 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[setTimeout]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=972</guid>
		<description><![CDATA[In my last two tutorials, I explained how to use event delegation for showing and hiding tooltips. In a comment on the first one, Jan Aagaard asked how we might go about enhancing the script by adding a small delay before showing a tooltip. The answer lies with two JavaScript functions, setTimeout() and clearTimeout(). Setting [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements" title="
Simple Tooltip for Huge Number of Elements">last</a> <a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips" title="
Binding Multiple Events to Reduce Redundancy with Event-Delegation">two</a> tutorials, I explained how to use event delegation for showing and hiding tooltips. In a comment on the first one, <a href="http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements/#div-comment-79280">Jan Aagaard asked</a> how we might go about enhancing the script by adding a small delay before showing a tooltip. The answer lies with two JavaScript functions, <code>setTimeout()</code> and <code>clearTimeout()</code>. </p>
<span id="more-972"></span>
<h4>Setting Up</h4>
<p>Picking up where we left off <a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips" title="
Binding Multiple Events to Reduce Redundancy with Event-Delegation">last time</a>, I'm going to create the tooltip div, declare a couple variables, and then bind "mouseover," "mousemove," and "mouseout" all at once. In addition to <code>$livetip</code> and <code>tipTitle</code>, I'm declaring <code>showTip</code> (and leaving it undefined for the moment) and <code>delay</code>. The <code>showTip</code> variable will be used as a reference for the <code>setTimeout</code> function, and <code>delay</code> will be used to set the time after which the function within <code>setTimeout</code>'s argument will execute (in this case, 300ms).</p>
<div class="igBar"><span id="ljavascript-34"><a href="#" onclick="javascript:showPlainTxt('javascript-34'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-34">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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; showTip<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=delay"><span style="">delay</span></a> <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</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: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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;">// ... code continues</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>Of course, you don't <em>have to</em> use a variable for the delay, but I like to have that sort of setting up top where I can find it easily. Also, if I ever decide to convert this script into a plugin, having the delay stored in a variable will make it simpler to convert it to an option.</p>
<h4>Setting the Timeout</h4>
<p>Inside the <code>.bind()</code>, and after the business of determining whether the event is being triggered by a link or one of its descendants, the <code>setTimeout</code> is called on line 12:</p>
<div class="igBar"><span id="ljavascript-35"><a href="#" onclick="javascript:showPlainTxt('javascript-35'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-35">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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; showTip<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=delay"><span style="">delay</span></a> <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</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: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span>; <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: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</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; showTip <span style="color: #339933;">=</span> window.<span style="color: #660066;">setTimeout</span><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; $link.<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;">'tipActive'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; tipTitle <span style="color: #339933;">=</span> link.<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; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip</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: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<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; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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: #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; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=delay"><span style="">delay</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; <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;">// ... code continues</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><code>setTimeout</code> is actually a method of the <code>window</code> object; as such, the <code>this</code> keyword inside its function argument will refer to <code>window</code>. It's not of much concern here because, with event delegation, we're relying on <code>event.target</code> rather than <code>this</code>, but I thought it might be worth mentioning anyway. </p>
<p>Note that within the <code>setTimeout</code> function, I'm storing a little data (line 13) for the link to indicate that the tooltip is active on it. This <code>tipActive</code> information then determines what to do on mouseout.</p>
<h4>Clearing the Timeout</h4>
<p>On mouseout, if the link has the <code>tipActive</code> data, that data is removed (line 29, below), the tooltip is hidden (line 30), and the link's original title attribute value is restored if tipTitle has a value (line 31). If that <code>tipActive</code> data is <em>not</em> associated with the link, then the timeout that was set on mouseover is reset with <code>clearTimeout</code> (line 33).</p>
<div class="igBar"><span id="ljavascript-36"><a href="#" onclick="javascript:showPlainTxt('javascript-36'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-36">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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; showTip<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=delay"><span style="">delay</span></a> <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</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: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span>; <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: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</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; showTip <span style="color: #339933;">=</span> window.<span style="color: #660066;">setTimeout</span><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; $link.<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;">'tipActive'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; tipTitle <span style="color: #339933;">=</span> link.<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; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip</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: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<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; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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: #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; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=delay"><span style="">delay</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; <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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseout'</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;">if</span> <span style="color: #009900;">&#40;</span>$link.<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;">'tipActive'</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; $link.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=removeData"><span style="">removeData</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</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; $liveTip.<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>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> tipTitle <span style="color: #339933;">||</span> link.<span style="color: #660066;">title</span>; &nbsp; &nbsp; &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;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</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; window.<span style="color: #660066;">clearTimeout</span><span style="color: #009900;">&#40;</span>showTip<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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mousemove'</span> &#038;& $link.<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;">'tipActive'</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; $liveTip.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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>The mousemove part hasn't changed from what it was in the previous post, except that it first checks if the "tipActive" data is assigned to the link. I figured that setting the <code>top</code> and <code>left</code> properties of the tooltip is probably more work than doing a little check on the link's data, so why bother setting them if I don't have to?</p>
<p>So, that's it. Just add a simple <code>setTimeout</code> delay and conditionally clear it. If you want to set a delay on mouseout, you can do that the same way.</p>
<p><a href="http://test.learningjquery.com/tooltips/delaytip.html">Check out the demo</a> or <a href="http://test.learningjquery.com/tooltips/delaytip.zip">download the zip</a> (or neither, if you're stubborn).</p>

<h4>Flipping the Tip</h4>
<p>Next time, I'll show how to flip the tooltip from the right side of the cursor to the left if it would otherwise get cut off by the edge of the browser window. </p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Binding Multiple Events to Reduce Redundancy with Event-Delegation Tooltips</title>
		<link>http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips</link>
		<comments>http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:57:04 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[tooltips]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=965</guid>
		<description><![CDATA[Last time I showed how to use event delegation to create a simple tooltip for a huge number of elements without running into the problem of binding an event handler to all of those elements. In this tutorial, I'm going to refine that tooltip script a bit, avoiding some code repetition and fixing a bug [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I showed how to use event delegation to create a simple tooltip for a huge number of elements without running into the problem of binding an event handler to all of those elements. In this tutorial, I'm going to refine that tooltip script a bit, avoiding some code repetition and fixing a bug that someone pointed out in a comment.</p>
<span id="more-965"></span>
<p>For review, here is the final version of the script from the last post:</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: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</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> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><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;tipTitle <span style="color: #339933;">=</span> link.<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;link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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;left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;.<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;<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>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</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;$link.<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;">'title'</span><span style="color: #339933;">,</span> tipTitle<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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>;</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>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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;left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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 />

<h4>Bind Multiple Events at One Time</h4>
<p>As I mentioned last time, this repeats code where it really shouldn't. For example, lines 5, 19, and 24 all check that moused-over element (or one of its ancestors) is a link. One way to reduce the repetition is to bind all three events at the same time (line 4):</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> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$link.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span>; <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: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</span> <span style="color: #339933;">||</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mousemove'</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; $liveTip.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</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; tipTitle <span style="color: #339933;">=</span> link.<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; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $liveTip</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=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<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; <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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.type"><span style="">event.<span style="color: #660066;">type</span></span></a> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseout'</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; $liveTip.<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>;</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>tipTitle<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; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> tipTitle;</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 />
<p>This modification relies on the <code>type</code> property of the <code>event</code> object, checking it to see which of the three event types were triggered and acting accordingly. That way, I can get away with writing the <code>$link</code> and <code>link</code> declarations and the link check one time instead of two or three.</p>
<h4>Other changes</h4>
<p>Here are a few other changes worth mentioning:</p>
  <ul>
    <li>Line 1 declares the <code>$livetip</code> variable while at the same time creating the tooltip div and appending it to the body. Now the script can refer to that variable rather than having to call <code>$('#livetip')</code> each time it is shown, moved, hidden, and so on.</li>
    <li>Rather than wrap nearly everything in an <code>if</code> block, I made the function <code>return</code> if there is no link selected. Some people have a philosophical problem with multiple return points in a function, and it <em>can</em> make it hard to decipher and debug code sometimes, but here I think it's okay. </li>
    <li>On "mouseout" (lines 23-28), the script is resetting <code>link.title</code> to <code>tipTitle</code> only if <code>tipTitle</code> has a value. This, I'm hoping, fixes the <a href="http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements#div-comment-79289">problem reported by Moritz Peters</a> of the tooltip not displaying correctly if you first mouse over a link before the document is ready.</li>
  </ul>
  <p><a href="http://test.learningjquery.com/tooltips/drytip.html">Try the demo</a> or <a href="http://test.learningjquery.com/tooltips/drytip.html">download the zip</a> (or both).</p>
  <h4>Adding a Delay</h4>
  <p>Tomorrow&mdash;for realz this time&mdash;I'll show how to add a short delay on mouseover before showing the tooltip.</p>
<p>P.S. You see the "$" in "$liveTip" and "$link"? There is nothing special about that&mdash;no special meaning given to "$" in JavaScript. It's just a convention that some jQuery folks have adopted to remind ourselves that the variable is holding a jQuery object.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Simple Tooltip for Huge Number of Elements</title>
		<link>http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements</link>
		<comments>http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements#comments</comments>
		<pubDate>Tue, 01 Dec 2009 17:57:29 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[live]]></category>
		<category><![CDATA[tooltips]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=191</guid>
		<description><![CDATA[There are many, many jQuery tooltip plugins out there, and some of them are very good. But when someone on the jQuery Google Group asked (a year ago) which plugin could handle displaying tooltips for 2,000 links on a page, I wasn't able to find one. So, I decided to throw together a quick little [...]]]></description>
			<content:encoded><![CDATA[<p>There are many, many jQuery tooltip plugins out there, and some of them are very good. But when someone on the jQuery Google Group asked (a year ago) which plugin could handle displaying tooltips for 2,000 links on a page, I wasn't able to find one. So, I decided to throw together a quick little plugin myself and was surprised by how easy it was.</p>
<h4>Event Delegation, Again</h4>
<p>The key to having JavaScript handle hundreds, or even thousands, of elements on a page is to use event delegation. As Louis-Rémi Babé described in <a href="http://www.learningjquery.com/2009/09/working-with-events-part-3-more-event-delegation-with-jquery">Working with Events, Part 3: More Event Delegation with jQuery</a>, jQuery's <code>.live()</code> method makes event delegation dead easy. A simple tooltip script using <code>.live()</code> might look something like this:</p>
<span id="more-191"></span>
<div class="igBar"><span id="ljavascript-46"><a href="#" onclick="javascript:showPlainTxt('javascript-46'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-46">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=live"><span style="">live</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; tipTitle <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">title</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;">this</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<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;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=live"><span style="">live</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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;">this</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> tipTitle;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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>;</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 script does the following:</p>
<ul>
  <li>Creates a single tooltip element that will be shown and hidden as the user mouses over links (line 1)</li>
  <li>Hides the tooltip and appends it to the body (line 1)</li>
  <li>Changes the tooltip's contents according to the moused-over link's <code>title</code> and <code>href</code> attributes (lines 7 and 14)</li>
  <li>Places the tooltip on the page 12px below and to the right of the cursor position at the time that it entered the link (lines 9&ndash;13)</li>
  <li>Shows the tooltip (line 15)</li>
  <li>Sets the link's <code>title</code> attribute to an empty string when the user mouses over the link; this prevents the browser's default tooltip from appearing (line 8)</li>
  <li>Sets the link's title back to what it was originally and hides the tooltip when the user mouses out of the link (lines 16&ndash;19)</li>
</ul>
<h4>A Little Style</h4>
<p>At minimum, the tooltip needs to have the <code>position: absolute</code> style declaration for it to be positioned correctly on the page, but I threw in a little extra CSS to make it look more appealing:</p>
<div class="igBar"><span id="lcss-47"><a href="#" onclick="javascript:showPlainTxt('css-47'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">CSS:</span><br /><div id="css-47">
<div class="css" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #cc00cc;">#livetip <span style="color: #66cc66;">&#123;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <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;">&nbsp; <span style="color: #000000;">background-color</span><span style="color: #66cc66;">:</span> #cfc<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">border</span><span style="color: #66cc66;">:</span> 2px <span style="color: #993333;">solid</span> #c9c<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; border-radius<span style="color: #66cc66;">:</span> 4px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; -webkit-border-radius<span style="color: #66cc66;">:</span> 4px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; -moz-border-radius<span style="color: #66cc66;">:</span> 4px<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>
</ol></div>
</div></div><br />
<p>Typically I like to include live demos directly in the blog entry, but since this one involves 2,000 links, I've set up separate demo pages. Check out the <a href="http://test.learningjquery.com/tooltips/livetip.html">tooltip demo using <code>.live()</code></a>, or <a href="http://test.learningjquery.com/tooltips/livetip.zip">download the zip</a>. </p>
<h4>A Little Less Simple, A Little More Speedy</h4>
<p>Using <code>.live()</code> in this way avoids binding event handlers directly to the thousands of links on the page. However, it doesn't prevent jQuery from searching the entire document for all of those links. After all, I'm still using the <code>$('a')</code> jQuery function. Also, at least for now, <code>.live()</code> binds events to <code>document</code>, forcing event bubbling all the way up the DOM each time. If there is a noticeable performance lag when the script is initially executed, it may help to use custom event delegation.</p>
<div class="update">
<h4>Update</h4>
<p><strong>As of jQuery 1.4.2</strong>, the <strong>.delegate()</strong> method can take the place of custom event delegation. <strong>As of jQuery 1.7</strong>, the <code>.on()</code> method can take the place of all other event methods, including <code>.live()</code> and <code>.delegate()</code>.
</p></div>
<p> Using custom event delegation, I bind the events to a containing table element:</p>
<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showPlainTxt('javascript-48'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-48">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</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> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</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; tipTitle <span style="color: #339933;">=</span> link.<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; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<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; <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>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</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; $link.<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;">'title'</span><span style="color: #339933;">,</span> tipTitle<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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>;</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>With this approach, I needed to first make sure I was dealing with a link before I tried to do anything with it. Line 5 uses the <code>.closest()</code> method to select the <code>&lt;a&gt;</code> that is either the <code>event.target</code> or one of its ancestors. Line 6 checks the <code>length</code> property to see if any elements are included in the variable declared in line 5. </p>
<p>Note the use of <code>.bind()</code> instead of <code>.live()</code> and <code>event.target</code> instead of <code>this</code>. I could have used the <code>.mouseover()</code> and <code>.mouseout()</code> shortcut methods, but since I used <code>.live()</code> in the first example, it was easier to simply replace "live" with "bind." Also, you may be wondering why I used mouseover / mouseout when I could have used mouseenter / mouseleave or even the <code>.hover()</code> method (which uses mouseenter and mouseleave internally). The reason is that mouseenter and mouseleave prevent the event bubbling that this script relies on for its event delegation. Those two events would be triggered only when the mouse enters or leaves the full table, while mouseover and mouseout are triggered whenever the mouse enters or leaves <em>any of the table's descendant elements</em>, as well.</p>
<p>View the <a href="http://test.learningjquery.com/tooltips/delegatip.html">demo using custom event delegation</a>, or <a href="http://test.learningjquery.com/tooltips/delegatip.zip">download the zip</a>.</p>
<h4>One more Enhancement</h4>
<p>Some people like having the tooltip move along with the cursor, as long as the mouse is over the element. Sometimes it's nice to be able move the tooltip a little if it initially overlaps something important. Adding the basic functionality is pretty straightforward. Just bind the <code>mousemove</code> event to the same table element:</p>
<div class="igBar"><span id="ljavascript-49"><a href="#" onclick="javascript:showPlainTxt('javascript-49'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-49">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</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>.<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;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</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: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</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> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</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; tipTitle <span style="color: #339933;">=</span> link.<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; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<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; <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>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<span style="color: #660066;">length</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; $link.<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;">'title'</span><span style="color: #339933;">,</span> tipTitle<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</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>;</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>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<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: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#livetip'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageY"><span style="">event.<span style="color: #660066;">pageY</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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; left<span style="color: #339933;">:</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.pageX"><span style="">event.<span style="color: #660066;">pageX</span></span></a> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</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>With the addition of lines 25 through 32, as the mouse moves over the links, the tooltip's top and left style properties are continually updated based on the mouse's position. </p>
<p><a href="http://test.learningjquery.com/tooltips/movetip.html">Try the demo</a> and watch the tooltip move along with the mouse position, or <a href="http://test.learningjquery.com/tooltips/movetip.zip">download the zip</a>.</p>
<h4>The Plugin</h4>
<p>The plugin doesn't offer much more than the scripts in this post, but feel free to <a href="http://plugins.learningjquery.com/eztip/demo/">try it out</a>. One difference is that it doesn't rely on the <code>.closest()</code> method, so it can be used with jQuery 1.2.6 if you're stuck with that version for some reason. The syntax is a little funky:  </p>
<div class="igBar"><span id="ljavascript-50"><a href="#" onclick="javascript:showPlainTxt('javascript-50'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-50">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>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;">&nbsp; $<span style="color: #009900;">&#40;</span>containerElement<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eztip</span><span style="color: #009900;">&#40;</span>invokingElement<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>optionsMap<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;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>The <code>containerElement</code> selector provides context so you can specify where exactly the event handlers are bound (unlike <code>.live()</code>, which binds to the document). The first argument of <code>.eztip()</code> is required. It should be a selector that matches the elements for which you wish to show a tooltip. The second argument is the typical options object, allowing for a handful of objects that you might expect to find in a stripped down tooltip plugin.</p>
<h4>Let's Make Better Mistakes Tomorrow</h4>
<p>The tooltip scripts in this post work fine, but they repeat code where they really shouldn't. Stay tuned tomorrow for a brief tutorial on one way to avoid such code repetition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements/feed</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
		<item>
		<title>Working with Events, Part 3: More Event Delegation with jQuery</title>
		<link>http://www.learningjquery.com/2009/09/working-with-events-part-3-more-event-delegation-with-jquery</link>
		<comments>http://www.learningjquery.com/2009/09/working-with-events-part-3-more-event-delegation-with-jquery#comments</comments>
		<pubDate>Wed, 23 Sep 2009 09:36:27 +0000</pubDate>
		<dc:creator>Louis-Rémi Babé</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=907</guid>
		<description><![CDATA[Event delegation, as described in the first article of this series, is a way to take advantage of event bubbling to avoid binding an event listener more than once. jQuery 1.3 and the upcoming jQuery 1.4 have many features that make using event delegation in your web pages easier. The aim of this tutorial is [...]]]></description>
			<content:encoded><![CDATA[<p>
  Event delegation, as described in the <a href="http://www.learningjquery.com/2008/03/working-with-events-part-1">first article of this series</a>, is a way to take advantage
  of event bubbling to avoid binding an event listener more than once. jQuery 1.3 and the upcoming jQuery 1.4
  have many features that make using event delegation in your web pages easier. 
  The aim of this tutorial is to help you understand how these new features work. <span id="more-907"></span>
</p>

<h4>From traditional event listening to event delegation</h4>
<p>
  Since an event occurring on an element is propagated to all of its ancestors,
  an event listener can be bound to a single ancestor of numerous elements instead
  of being bound to all the elements individually.
</p>
<p>
  Consider the following list items:
</p>
<div class="igBar"><span id="lhtml-62"><a href="#" onclick="javascript:showPlainTxt('html-62'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-62">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myList&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; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #000000;">&gt;</span></span>The first item.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;green&quot;</span><span style="color: #000000;">&gt;</span></span>The second item.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;yellow&quot;</span><span style="color: #000000;">&gt;</span></span>The third item.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blue&quot;</span><span style="color: #000000;">&gt;</span></span>The fourth item.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;p&gt;</span></span>Class of the last clicked item: <span style="color: #009900;"><span style="color: #000000;">&lt;span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display&quot;</span><span style="color: #000000;">&gt;</span></span> <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;"><span style="color: #009900;"><span style="color: #000000;">&lt;/p&gt;</span></span></div></li>
</ol></div>
</div></div><br />
<p>
  If we want to display the class of an item when it is clicked,
  a traditional event listener in jQuery would be written like this:
</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;li&quot;</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> event <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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a>.<span style="color: #660066;">className</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>
  The event object passed to the handler has a <code>target</code> property which corresponds
  to the element that has been clicked.
</p>
<p>
  The equivalent using event delegation would look like this:
</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;ul&quot;</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> event <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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a>.<span style="color: #660066;">className</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 />
<div id="test1" class="events-test">
  <ul class="myList">
    <li class="red">The first item.</li>
    <li class="green">The second item.</li>
    <li class="yellow">The third item.</li>
    <li class="blue">The fourth item.</li>

  </ul>
  <p>Class of the last clicked item: <span class="display"></span></p>
</div>
<p>
  Event delegation has two main advantages: 
  <ol>
    <li>setting a single event listener instead of multiple ones is obviously faster;</li>
    <li>any new element later added to the list will have the same behavior (as demonstrated in <a href="http://www.learningjquery.com/2008/03/working-with-events-part-1">Working with Events, Part 1</a>).</li>
  </ol>
</p>
<p>
  This <em>translation</em> to event delegation that we've just made is, however, slightly too simple,
  since our original aim was to display only the class of the list items. Using the previous
  snippet, the class of the unordered list itself can be displayed (by clicking to the left of a bullet-point). 
  We thus have to make sure that the target of the click is a <code>&lt;li&gt;</code>.
</p>

<div class="igBar"><span id="ljavascript-65"><a href="#" onclick="javascript:showPlainTxt('javascript-65'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-65">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</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> event <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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a>.<span style="color: #660066;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;LI&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a>.<span style="color: #660066;">className</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 />
<div id="test2" class="events-test">
  <ul class="myList">
    <li class="red">The first item.</li>
    <li class="green">The second item.</li>
    <li class="yellow">The third item.</li>

    <li class="blue">The fourth item.</li>
  </ul>
  <p>Class of the last clicked item: <span class="display"></span></p>
</div>
<h4>Scanning the ancestors of the event.target: the <code>.closest()</code> method</h4>
<p>

  With such a simple document, event delegation is really that easy to achieve.
  But things can get more complex if the items have children elements:
</p>
<div class="igBar"><span id="lhtml-66"><a href="#" onclick="javascript:showPlainTxt('html-66'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-66">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myList&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; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;red&quot;</span>&gt;&lt;b&gt;</span>The <span style="color: #009900;"><span style="color: #000000;">&lt;i&gt;</span></span>first <span style="color: #009900;"><span style="color: #000000;">&lt;u&gt;</span></span>item<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>u&gt;&lt;<span style="color: #66cc66;">/</span>i&gt;&lt;<span style="color: #66cc66;">/</span>b&gt;</span>.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;green&quot;</span>&gt;&lt;b&gt;</span>The <span style="color: #009900;"><span style="color: #000000;">&lt;i&gt;</span></span>second <span style="color: #009900;"><span style="color: #000000;">&lt;u&gt;</span></span>item<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>u&gt;&lt;<span style="color: #66cc66;">/</span>i&gt;&lt;<span style="color: #66cc66;">/</span>b&gt;</span>.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;yellow&quot;</span>&gt;&lt;b&gt;</span>The <span style="color: #009900;"><span style="color: #000000;">&lt;i&gt;</span></span>third <span style="color: #009900;"><span style="color: #000000;">&lt;u&gt;</span></span>item<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>u&gt;&lt;<span style="color: #66cc66;">/</span>i&gt;&lt;<span style="color: #66cc66;">/</span>b&gt;</span>.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blue&quot;</span>&gt;&lt;b&gt;</span>The <span style="color: #009900;"><span style="color: #000000;">&lt;i&gt;</span></span>fourth <span style="color: #009900;"><span style="color: #000000;">&lt;u&gt;</span></span>item<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>u&gt;&lt;<span style="color: #66cc66;">/</span>i&gt;&lt;<span style="color: #66cc66;">/</span>b&gt;</span>.<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
<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: #009900;"><span style="color: #000000;">&lt;p&gt;</span></span>Class of the last clicked item: <span style="color: #009900;"><span style="color: #000000;">&lt;span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display&quot;</span><span style="color: #000000;">&gt;</span></span> <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;"><span style="color: #009900;"><span style="color: #000000;">&lt;/p&gt;</span></span></div></li>
</ol></div>
</div></div><br />
<p>
  In this case, if the user clicks on one of the words <em>item</em>, the <code>event.target</code>
  will be a <code>&lt;u&gt;</code>. It is therefore necessary to loop through all of the ancestors
  of this original target to find the element that is "interesting" for us: the <code>&lt;li&gt;</code>

</p>
<div class="igBar"><span id="ljavascript-67"><a href="#" onclick="javascript:showPlainTxt('javascript-67'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-67">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</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> event <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> elem <span style="color: #339933;">=</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a>;</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;">while</span><span style="color: #009900;">&#40;</span> elem.<span style="color: #660066;">nodeName</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;LI&quot;</span> &#038;& elem.<span style="color: #660066;">parentNode</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; elem <span style="color: #339933;">=</span> elem.<span style="color: #660066;">parentNode</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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">nodeName</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;LI&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a>.<span style="color: #660066;">className</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>
  Notice that if the user clicks outside of an <code>&lt;li&gt;</code>, we have to stop looping
  through the ancestors at some point; in this case when we find the root of the document
  (which has no <code>parentNode</code>). 
</p>

<p>
  jQuery 1.3 introduced the <code>.closest()</code> method, which replaces this loop by a single line of code:
</p>
<div class="igBar"><span id="ljavascript-68"><a href="#" onclick="javascript:showPlainTxt('javascript-68'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-68">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</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> event <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> $elem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&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; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$elem.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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;">&quot;class&quot;</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>;</div></li>
</ol></div>
</div></div><br />
<div id="test3" class="events-test">
  <ul class="myList">
    <li class="red"><b>The <i>first <u>item</u></i></b>.</li>
    <li class="green"><b>The <i>second <u>item</u></i></b>.</li>
    <li class="yellow"><b>The <i>third <u>item</u></i></b>.</li>

    <li class="blue"><b>The <i>fourth <u>item</u></i></b>.</li>
  </ul>
  <p>Class of the last clicked item: <span class="display"></span></p>
</div>
<p>
  The parameter passed to the <code>.closest()</code> method is a CSS selector that will match
  the first <em>interesting</em> ancestor. 
</p>

<h4 id="context">The context parameter</h4>
<p>
  It can be noted that, when the click occurs outside of an <code>&lt;li&gt;</code>, we could
  stop looping through the ancestors before hitting the document root, but instead
  as soon as we hit the <code>&lt;ul&gt;</code>. jQuery 1.4 will introduce an optional <em>context</em> parameter 
  to <code>.closest()</code> for this purpose:
</p>

<div class="igBar"><span id="ljavascript-69"><a href="#" onclick="javascript:showPlainTxt('javascript-69'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-69">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</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> event <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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #339933;">,</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=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</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: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>
  Here <code>this</code> is the <code>&lt;ul&gt;</code> element to which the event listener has been bound. 
  This optional parameter improves the performance of event delegation as it prevents
  the wasted resources of searching for an ancestor which cannot exist.  
</p>
<h4>Event delegation in a single line: the <code>.live()</code> method</h4>

<p>
  jQuery 1.3 also introduces <code>.live()</code>, a method which binds the event listener and
  implicitly calls the <code>.closest()</code> method to determine whether your event handler should
  be executed or not.
</p>
<div class="igBar"><span id="ljavascript-70"><a href="#" onclick="javascript:showPlainTxt('javascript-70'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-70">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=live"><span style="">live</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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></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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.currentTarget"><span style="">event.<span style="color: #660066;">currentTarget</span></span></a><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;">&quot;class&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; <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>
  Note how different the syntax using the <code>.live()</code> method is. 
  It seems that we have switched back to a traditional event binding. However, a big difference
  is that the event listener will work for any existing and future element matching our original selector.
  There is, of course, nothing magic: behind the scenes <code>.live()</code> simply binds the event listener
  to the document root and filters any <code>event.target</code> using the <code>.closest()</code> method.
</p>
<p>
  A notable difference with the traditional event binding syntax is that we are not using the <code>target</code>
  property of the event object inside our event handler, but rather the <code>currentTarget</code>. Indeed, the target of the event
  is possibly a child of one <code>&lt;li&gt;</code>, whereas the <code>currentTarget</code> property refers to the element
  that has been found by the implicit <code>.closest()</code>. 
</p>
<p>

  As explained on the <a href="http://docs.jquery.com/Events/jQuery.Event">Event Object documentation</a>, the <code>currentTarget</code> property is supposed 
  to be <q>the current DOM element within the event bubbling phase</q>, i.e. the element on which the 
  event has been <em>detected</em> by the event listener, which will always be the element on which the 
  event listener was bound. When using <code>.live()</code>, the <code>currentTarget</code> would therefore always
  be the document root and you would need to use <code>.closest()</code> once again on the original <code>target</code> to find
  an interesting ancestor.
</p>

<div class="igBar"><span id="ljavascript-71"><a href="#" onclick="javascript:showPlainTxt('javascript-71'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-71">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=live"><span style="">live</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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></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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.target"><span style="">event.<span style="color: #660066;">target</span></span></a><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=closest"><span style="">closest</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</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;">&quot;class&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; <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>
  This kind of code is required with jQuery 1.3, but as of jQuery 1.4, the <code>currentTarget</code> is modified internally
  and can thus be used to avoid the extra <code>.closest()</code> inside the event handler.
</p>
<h4 id="liveContext">The context parameter</h4>

<p>
  In jQuery 1.3 all live event listeners were effectively bound to the document root.
  This can hurt the performance of a web page since all events <em>detected</em>
  by an event listener will trigger the execution of an implicit <code>.closest()</code>,
  even though the event target might be totally out of interest.
</p>
<p>
  In jQuery 1.4, <code>.live()</code> should be able to bind the event listener to a specific and more
  focused element of the document by taking advantage of the <em>context</em> of your jQuery object:
</p>

<div class="igBar"><span id="ljavascript-72"><a href="#" onclick="javascript:showPlainTxt('javascript-72'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-72">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=live"><span style="">live</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</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></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><a href="/wp-content/themes/ljq/docs-1.7.php?fn=event.currentTarget"><span style="">event.<span style="color: #660066;">currentTarget</span></span></a><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;">&quot;class&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; <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>
  The context is the second parameter used to build our jQuery object. To be useful for <code>.live()</code>
  it has to be set as a pure DOM element, hence the <code>[0]</code> after <code>$("ul")</code>.
  Brandon Aaron has a useful article <a href="http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery">explaining the context parameter</a> in detail.
</p>

<h4>Unbind for <code>.live()</code>: the <code>.die()</code> method</h4>
<p>
  Just like <code>.bind()</code> has its <code>.unbind()</code> counterpart that allows for event listeners
  to be unbound, <code>.live()</code> has its <code>.die()</code> counterpart.
</p>
<p>
  Unlike <code>.bind()</code>, <code>.live()</code> does not allow for <a href="http://docs.jquery.com/Namespaced_Events">namespaced events</a> to be used.
</p>
<h4>Dealing with events that don't bubble</h4>
<p>
  There are some events for which event delegation is traditionally not possible, simply because they do not bubble.
  The promise of jQuery 1.4 is nevertheless to make those events compatible with <code>.live()</code>.
  How this is achieved will be covered in the upcoming fourth part of this tutorial.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/09/working-with-events-part-3-more-event-delegation-with-jquery/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Tab Navigation with Smooth Horizontal Sliding Using jQuery</title>
		<link>http://www.learningjquery.com/2009/09/tab-navigation-with-smooth-horizontal-sliding-using-jquery</link>
		<comments>http://www.learningjquery.com/2009/09/tab-navigation-with-smooth-horizontal-sliding-using-jquery#comments</comments>
		<pubDate>Fri, 18 Sep 2009 12:19:28 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[animations]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=896</guid>
		<description><![CDATA[In this tutorial I'll show you how to create a navigation menu that slides horizontally. It begins with a set of "tabs" on the right side of a containing element. When clicked, a tab slides to the left to reveal a group of links. Click the tab again, and it slides back. While I've never [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial I'll show you how to create a navigation menu that slides horizontally. It begins with a set of "tabs" on the right side of a containing element. When clicked, a tab slides to the left to reveal a group of links. Click the tab again, and it slides back. While I've never had a need to build one of these for any of my own projects, quite a few people have asked if I would demonstrate how it might be done, so here goes. <span id="more-896"></span></p>
<div class="update">
  <h4>Download</h4>
  <p>You can now <a href="http://assets.learningjquery.com/zips/tab-nav.zip">download a .zip</a> of complete, working demos featured in this post.</p>
</div>
<h4>The Styles</h4>
<p>For the navigation items, I used a simple unordered list wrapped in a <code>&lt;div class="nav"&gt;</code>. While the HTML structure is straightforward, the CSS is a little tricky, so I'll detail it here:</p>
<!--more-->
<div class="igBar"><span id="lcss-79"><a href="#" onclick="javascript:showPlainTxt('css-79'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">CSS:</span><br /><div id="css-79">
<div class="css" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #808080; font-style: italic;">/* nav wrapper */</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.tab-nav </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">position</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">relative</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">width</span><span style="color: #66cc66;">:</span> 610px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">overflow</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">hidden</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">background</span><span style="color: #66cc66;">:</span> #ddd <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span>tab-slide.png<span style="color: #66cc66;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #cc66cc;color:#800000;">0</span> <span style="color: #cc66cc;color:#800000;">0</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: #808080; font-style: italic;">/* nav */</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.tab-nav </span>ul <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">position</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">relative</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">float</span><span style="color: #66cc66;">:</span> <span style="color: #000000;">left</span><span style="color: #66cc66;">;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">width</span><span style="color: #66cc66;">:</span> 1600px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">margin-left</span><span style="color: #66cc66;">:</span> 535px<span style="color: #66cc66;">;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">padding-left</span><span style="color: #66cc66;">:</span> <span style="color: #cc66cc;color:#800000;">0</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">list-style-type</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;">&nbsp; <span style="color: #000000;">background-color</span><span style="color: #66cc66;">:</span> #fff<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;">.tab-nav </span>li <span style="color: #66cc66;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">float</span><span style="color: #66cc66;">:</span> <span style="color: #000000;">left</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">clear</span><span style="color: #66cc66;">:</span> <span style="color: #000000;">left</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;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.tab-nav </span>a <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">display</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">block</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">width</span><span style="color: #66cc66;">:</span> 74px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">border-right</span><span style="color: #66cc66;">:</span> 1px <span style="color: #993333;">solid</span> #ddd<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">height</span><span style="color: #66cc66;">:</span> 25px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">line-height</span><span style="color: #66cc66;">:</span> 24px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">float</span><span style="color: #66cc66;">:</span> <span style="color: #000000;">left</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">text-align</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">center</span><span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <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;">&nbsp; <span style="color: #000000;">color</span><span style="color: #66cc66;">:</span> #000<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">background</span><span style="color: #66cc66;">:</span> &nbsp;<span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span>tab-slide.png<span style="color: #66cc66;">&#41;</span> <span style="color: #993333;">no-repeat</span> 2px -194px<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;">.tab-nav </span>a<span style="color: #6666ff;">.<span style="color: #993333;">expanded</span> </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">background-position</span><span style="color: #66cc66;">:</span> 2px -244px<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: #808080; font-style: italic;">/* second-level overrides */</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #6666ff;">.tab-nav </span>ul ul <span style="color: #66cc66;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">float</span><span style="color: #66cc66;">:</span> <span style="color: #000000;">left</span><span style="color: #66cc66;">;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">background-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;">&nbsp; <span style="color: #000000;">width</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">auto</span><span style="color: #66cc66;">;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000000;">margin-left</span><span style="color: #66cc66;">:</span> <span style="color: #cc66cc;color:#800000;">0</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;">.tab-nav </span>li li <span style="color: #66cc66;">&#123;</span><span style="color: #000000;">clear</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">none</span><span style="color: #66cc66;">;</span><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;">.tab-nav </span>li li a <span style="color: #66cc66;">&#123;</span> <span style="color: #000000;">color</span><span style="color: #66cc66;">:</span> #fff<span style="color: #66cc66;">;</span> <span style="color: #000000;">width</span><span style="color: #66cc66;">:</span> 100px<span style="color: #66cc66;">;</span> <span style="color: #000000;">background-image</span><span style="color: #66cc66;">:</span> <span style="color: #993333;">none</span><span style="color: #66cc66;">;</span><span style="color: #66cc66;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>Most of the relevant CSS here has to do with positioning the nav items. I set the top <code>&lt;ul&gt;</code>'s left margin to 75 pixels less than the wrapper's width so that the top-level links appear on the right side. The 1600px width for the <code>&lt;ul&gt;</code> gives the floated list items ample room to line up horizontally next to each other.</p>
<p>The wrapper's <code>overflow</code> declaration is significant, as it hides the list items  when they're sticking out to the right, but the rest is "window dressing."</p>

<h4>Sliding the Nav</h4>

<p>With the nav looking the way I want it at its initial state, it's time to make it do something. I'll start with a simple setup, having each "tab" (top-level item) slide to the left on the first click to reveal its sub-nav items, and slide back to its initial position when it's clicked a second time.</p>
<p>For this basic behavior, everything can be done inside a <code>click</code> handler for the top-level links. <strong>Note:</strong> Since I'm using multiple navs for this tutorial, each with its own set of behavior, I'll be referring to them by ID, unlike in the CSS snippet above, where everything is styled by class. There is nothing special about the selectors or their naming here. Name your own elements and select them however you want.</p>

<p>The first thing to do is set a few variables. The <code>$parentItem</code> variable is the <code>&lt;li&gt;</code> parent of the clicked link. The <code>slideAmt</code> is the width of the nested <code>&lt;ul&gt;</code>, which is the next sibling of the link. And <code>direction</code> will eventually determine whether the parent <code>&lt;li&gt;</code> should be slid to the left or to the right.</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;"><span style="color: #003366; font-weight: bold;">var</span> $topLinks1 <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-1 &gt; ul &gt; li &gt; a'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$topLinks1.<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: #003366; font-weight: bold;">var</span> $parentItem <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=parent"><span style="">parent</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; slideAmt <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=next"><span style="">next</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=width"><span style="">width</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; direction;</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;">// code continues</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>Notice the use of <code>$(this)</code>. Inside the <code>click</code> handler, <code>this</code> refers to the clicked DOM element. By wrapping <code>this</code> in <code>$()</code>, we can call jQuery methods on it.</p>
<p>To get the sliding motion to occur, we can animate either the <code>left</code> property or the <code>marginLeft</code> property. Here, I'll animate <code>marginLeft</code>. So, the next thing to do is determine the direction of the animation based on the current value of <code>marginLeft</code>: If it's less than 0, <code>direction</code> is set to "+=", which increases it (back to 0); otherwise, <code>direction</code> is set to "-=". At the same time, an "expanded" class will be toggled so that the arrow background image can change directions.</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;"><span style="color: #003366; font-weight: bold;">var</span> $topLinks1 <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-1 &gt; ul &gt; li &gt; a'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$topLinks1.<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: #003366; font-weight: bold;">var</span> $parentItem <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=parent"><span style="">parent</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; slideAmt <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=next"><span style="">next</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=width"><span style="">width</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; direction;</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>$parentItem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marginLeft'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#41;</span> &lt;<span style="color: #CC0000;color:#800000;">0</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+='</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/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;">'expanded'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/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;">'expanded'</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-='</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;">// code continues</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Finally, we do the animation, plugging in the <code>direction</code> and <code>slideAmt</code> variables. The <code>return false;</code> line stops the default click action from occurring. Here is the finished code for the basic implementation:</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;"><span style="color: #003366; font-weight: bold;">var</span> $topLinks1 <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-1 &gt; ul &gt; li &gt; a'</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; $topLinks1.<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; <span style="color: #003366; font-weight: bold;">var</span> $parentItem <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=parent"><span style="">parent</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; &nbsp; slideAmt <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=next"><span style="">next</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=width"><span style="">width</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; &nbsp; direction;</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>$parentItem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marginLeft'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#41;</span> &lt;<span style="color: #CC0000;color:#800000;">0</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+='</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=removeClass"><span style="">removeClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'expanded'</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</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;">'expanded'</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-='</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; $parentItem</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=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>marginLeft<span style="color: #339933;">:</span> direction <span style="color: #339933;">+</span> slideAmt<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">400</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: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Give it a try:</p>

<div id="tab-nav-1" class="tab-nav">
  <ul>
    <li>
      <a href="#">one</a>
      <ul>
        <li><a href="#">sub one</a></li>
        <li><a href="#">sub two</a></li>
        <li><a href="#">sub three</a></li>
      </ul>
    </li>
    <li>
      <a href="#">two</a>
      <ul>
        <li><a href="#">b sub one</a></li>
        <li><a href="#">b sub two</a></li>
        <li><a href="#">b sub three</a></li>
        <li><a href="#">b sub four</a></li>
        <li><a href="#">b sub five</a></li>
      </ul>
    </li>
    <li>
      <a href="#">three</a>
      <ul>
        <li><a href="#">sub one</a></li>
        <li><a href="#">sub two</a></li>
        <li><a href="#">sub three</a></li>
      </ul>
    </li>
  </ul>
</div>

<h4>One at a Time</h4>
<p>That's all well and good, but I don't really care for having more than one row of items expanded at a time. A couple simple modifications will fix that for us: </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;">$<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;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> $topLinks2 <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-2 &gt; ul &gt; li &gt; a'</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; $topLinks2.<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; <span style="color: #003366; font-weight: bold;">var</span> $parentItem <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=parent"><span style="">parent</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; &nbsp; slideAmt <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=next"><span style="">next</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=width"><span style="">width</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; &nbsp; direction;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $topLinks2.<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;">'expanded'</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>parseInt<span style="color: #009900;">&#40;</span>$parentItem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marginLeft'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#41;</span> &lt;<span style="color: #CC0000;color:#800000;">0</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+='</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</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;">'expanded'</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-='</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; $parentItem</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=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>marginLeft<span style="color: #339933;">:</span> direction <span style="color: #339933;">+</span> slideAmt<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">400</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=siblings"><span style="">siblings</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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>marginLeft<span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">150</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: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: 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 links are stored in a variable first thing here. Whenever a link is clicked, all links have the "expanded" class removed. Of course, only one, at most, will have that class, but I'm lazy and it's easier to tell all of the links to remove the class than it is to hunt links that have the class first.
</p><p>The same check is performed to set the direction of the animation. If the clicked link's parent <code>&lt;li&gt;</code> is going to be slid to the left, the link will also get the "expanded" class.</p>
<p>Finally, the clicked link's parent is animated in the direction and number of pixels specified by the variables. But here's the twist: all of that parent's siblings have their <code>marginLeft</code> property animated to 0. Again, I'm taking the lazy route, animating all of the siblings, even though one or none of them will need it.</p>
<p>Here's a demo of the "one at a time" version:</p>
<div id="tab-nav-2" class="tab-nav">
  <ul>
    <li>
      <a href="#">one</a>
      <ul>
        <li><a href="#">sub one</a></li>
        <li><a href="#">sub two</a></li>
        <li><a href="#">sub three</a></li>
      </ul>
    </li>
    <li>
      <a href="#">two</a>
      <ul>
        <li><a href="#">b sub one</a></li>
        <li><a href="#">b sub two</a></li>
        <li><a href="#">b sub three</a></li>
        <li><a href="#">b sub four</a></li>
        <li><a href="#">b sub five</a></li>
      </ul>
    </li>
    <li>
      <a href="#">three</a>
      <ul>
        <li><a href="#">sub one</a></li>
        <li><a href="#">sub two</a></li>
        <li><a href="#">sub three</a></li>
      </ul>
    </li>
  </ul>
</div>

<h4>Auto-Collapse</h4>
<p>Now that the expanding and collapsing are happening the way I like it, I'll add one more little touch. If the user's mouse leaves the containing <code>&lt;div class="tab-nav"&gt;</code>, and stays out for a full second, any expanded list will collapse. </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>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;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> closeAll<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; $topLinks3 <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-3 &gt; ul &gt; li &gt; a'</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: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-3 ul ul'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'0.5'</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; $topLinks3.<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; <span style="color: #003366; font-weight: bold;">var</span> $parentItem <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=parent"><span style="">parent</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; &nbsp; slideAmt <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=next"><span style="">next</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=width"><span style="">width</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; &nbsp; direction;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $topLinks3.<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;">'expanded'</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>parseInt<span style="color: #009900;">&#40;</span>$parentItem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marginLeft'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#41;</span> &lt;<span style="color: #CC0000;color:#800000;">0</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+='</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</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;">'expanded'</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; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-='</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; $parentItem</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=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>marginLeft<span style="color: #339933;">:</span> direction <span style="color: #339933;">+</span> slideAmt<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">400</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=siblings"><span style="">siblings</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; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>marginLeft<span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">150</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: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: 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: #009900;">&#40;</span><span style="color: #3366CC;">'#tab-nav-3'</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=mouseleave"><span style="">mouseleave</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; closeAll <span style="color: #339933;">=</span> setTimeout<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; $topLinks3.<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;">'expanded'</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=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=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>marginLeft<span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">150</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> <span style="color: #CC0000;color:#800000;">1000</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: #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=mouseenter"><span style="">mouseenter</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; clearTimeout<span style="color: #009900;">&#40;</span>closeAll<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: 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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Line 2 declares a variable that will be used for the <code>setTimeout()</code>, which you can see tucked inside the <code>.mouseleave()</code> method near the bottom of the code. The <code>setTimeout()</code> function has two arguments: the first is an anonymous function that contains code for triggering the collapse of the list items, and the second is the number of milliseconds to wait (1,000 milliseconds) before the first argument (the anonymous function) is executed. The <code>clearTimeout()</code> inside the <code>.mouseenter()</code> method does what its name suggests: it clears the timeout. If the user mouses out of the nav area but then mouses back in before the 1000 milliseconds are up, the timer is stopped and the function will not be executed.</p>
<p><strong>Note:</strong> The <code>mouseenter(fn)</code> and <code>mouseleave(fn)</code> shorthand methods are available as of jQuery 1.3. If you're still using jQuery 1.2.6, you can use <code>.bind('mouseenter', fn)</code> and <code>.bind('mouseleave', fn)</code> instead. Or, with just about any version of jQuery, you can use <code>.hover(fn, fn)</code>.</p>
<p>Here is the final demo:</p>
<div id="tab-nav-3" class="tab-nav">
  <ul>
    <li>
      <a href="#">one</a>
      <ul>
        <li><a href="#">sub one</a></li>
        <li><a href="#">sub two</a></li>
        <li><a href="#">sub three</a></li>
      </ul>
    </li>
    <li>
      <a href="#">two</a>
      <ul>
        <li><a href="#">b sub one</a></li>
        <li><a href="#">b sub two</a></li>
        <li><a href="#">b sub three</a></li>
        <li><a href="#">b sub four</a></li>
        <li><a href="#">b sub five</a></li>
      </ul>
    </li>
    <li>
      <a href="#">three</a>
      <ul>
        <li><a href="#">sub one</a></li>
        <li><a href="#">sub two</a></li>
        <li><a href="#">sub three</a></li>
      </ul>
    </li>
  </ul>
</div>

<p>There are many ways to do this sort of thing. Just tweak the CSS or change the animation for a completely different experience. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/09/tab-navigation-with-smooth-horizontal-sliding-using-jquery/feed</wfw:commentRss>
		<slash:comments>77</slash:comments>
		</item>
		<item>
		<title>Setting up Visual Studio Intellisense for jQuery</title>
		<link>http://www.learningjquery.com/2009/07/setting-up-visual-studio-intellisense-for-jquery</link>
		<comments>http://www.learningjquery.com/2009/07/setting-up-visual-studio-intellisense-for-jquery#comments</comments>
		<pubDate>Thu, 16 Jul 2009 16:26:39 +0000</pubDate>
		<dc:creator>Ralph Whitbeck</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=822</guid>
		<description><![CDATA[In September 2008, at the jQuery Conference, it was announced that Microsoft would be shipping its upcoming releases of Visual Studio with jQuery built in. They would not fork or change the jQuery code but ship it as is. Microsoft also announced that they would distribute IntelliSense-annotated documentation JavaScript files for Visual Studio 2008 that [...]]]></description>
			<content:encoded><![CDATA[<p>In September 2008, at the jQuery Conference, it was announced that Microsoft would be shipping its upcoming releases of Visual Studio with jQuery built in. They would not fork or change the jQuery code but ship it as is.  Microsoft also announced that they would distribute IntelliSense-annotated documentation JavaScript files for Visual Studio 2008 that developers can reference in their files to enable IntelliSense for jQuery. The following instructions will help you get started using this tool to make writing jQuery scripts faster than ever.</p>
<span id="more-822"></span>
<h4>Setting up Your Files for jQuery IntelliSense</h4>

<p>First, set up jQuery on your page by downloading the latest version from the <a href="http://docs.jquery.com/Downloading_jQuery">jQuery site</a>.</p>

<p>Next, you will need to download the <a href="http://docs.jquery.com/Downloading_jQuery">jQuery Documentation file from the jQuery site</a>.</p>

<p><img src="http://learningjquery.kswedberg.netdna-cdn.com/wp-content/uploads/vs-download.png" alt="download documentation" width="379" height="132" class="alignnone" /></p>

<p>Download the files and add them to your project.</p>
<h4>IntelliSense in external JavaScript files</h4>
<p>At the top of the JavaScript file in which you would like to have jQuery IntelliSense enabled, you will need to add a line to reference the documentation file:</p>
<pre class="highlight"><code><strong class="comment">/// &lt;reference path="jquery-1.3.2-vsdoc2.js" /&gt;</strong></code></pre>
<h4>IntelliSense inline on your ASPX page</h4>
<p>There are a couple of scenarios that may affect if you include a reference to the VSDOC file or not.  If in doubt, just add a reference to the documentation file.</p>
<p>If you are linking to jQuery by the <a href="http://code.google.com/apis/ajaxlibs/">Google Code AJAX Library</a> or are linking to jQuery from anywhere outside of your project you will need to reference the documentation file.</p>
<p>From each ASPX page you want jQuery IntelliSense you’ll want to call the reference within a server-side conditional statement so that the documentation file will still load in Visual Studio but will not load at run-time. Add the following after your jQuery declaration:</p>
<div class="igBar"><span id="lasp-86"><a href="#" onclick="javascript:showPlainTxt('asp-86'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">ASP:</span><br /><div id="asp-86">
<div class="asp" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color:#0000FF; font-weight:bold;">&lt;%</span> <span style="color:#990099; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0000FF; font-weight:bold;">%&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.3.2-vsdoc2.js&quot;&gt;&lt;/script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color:#0000FF; font-weight:bold;">&lt;%</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#0000FF; font-weight:bold;">%&gt;</span></div></li>
</ol></div>
</div></div><br />
<p>If you downloaded jQuery and saved it to your project Visual Studio will look for the vsdoc.js file automatically if the following conditions are met.</p>
<p>You've downloaded and installed the <a href="http://blogs.msdn.com/webdevtools/archive/2008/11/07/hotfix-to-enable-vsdoc-js-intellisense-doc-files-is-now-available.aspx">hotfix</a> for Visual Studio.</p>

<p>jQuery and the documentation file need to be named the same with the exception that the documentation file end with -vsdoc.js.  So when you add jQuery to your project make sure to rename them similarly. For instance, <strong>jquery-1.3.2</strong>.js is your jQuery library, Visual Studio will look for the documentation file at <strong>jquery-1.3.2</strong>-vsdoc.js and load it.</p>
<p>(Note: the jQuery 1.3.2 documentation file is named jquery-1.3.2-vsdoc2.js on the Download page so make sure you take out the 2 so that the file will be found by Visual Studio)</p>
<p>If you can't meet these conditions you'll need to reference the documentation file as shown above.</p>
<p>To test to make sure the documentation file loaded correctly, you can type <code>$(</code> and you should be presented with some documentation.</p>
<p><img src="http://learningjquery.kswedberg.netdna-cdn.com/wp-content/uploads/vs-test.png" alt="test IntelliSense" width="576" height="96" class="alignnone" /></p>
<p>Additionally, you can type in <code>$(document).r</code> and you will be presented with a drop down of available options to choose from, one being "ready."</p>

<p><img src="http://learningjquery.kswedberg.netdna-cdn.com/wp-content/uploads/vs-docready.png" alt="test with document ready" width="541" height="169" class="alignnone" /></p>
<p>Now as you write your jQuery code, you'll be helped along by seeing what methods and properties are available to you.</p>
<h4>Related Links</h4>
<ul>
  <li><a href="http://blogs.msdn.com/webdevtools/archive/2008/10/28/rich-IntelliSense-for-jquery.aspx">Rich IntelliSense for jQuery</a></li>
  <li><a href="http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx">jQuery and Microsoft</a></li>
  <li><a href="http://blog.jquery.com/2008/09/28/jquery-microsoft-nokia/">jQuery, Microsoft and Nokia</a></li>
  <li><a href="http://blogs.msdn.com/webdevtools/archive/2008/11/07/hotfix-to-enable-vsdoc-js-intellisense-doc-files-is-now-available.aspx">VS2008 SP1 Hotfix to Support "-vsdoc.js" IntelliSense Doc Files</a></li>
</ul>
<div class="update">
  <h4>Update</h4>
 <p>I've updated the post to reflect the hotfix for Visual Studio which enables Visual Studio to automatically load the documentation file for inline jQuery on ASPX pages if it's named similarly to the jQuery file.  Thanks to Dave Ward for pointing this out in the comments.</p>
</div>
[inline][/inline]
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/07/setting-up-visual-studio-intellisense-for-jquery/feed</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/70 queries in 0.019 seconds using disk: basic
Object Caching 608/793 objects using disk: basic
Content Delivery Network via learningjquery.kswedberg.netdna-cdn.com

Served from: www.learningjquery.com @ 2012-02-07 05:35:00 -->
