<?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; Beginner</title>
	<atom:link href="http://www.learningjquery.com/category/levels/beginner/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>Sat, 06 Mar 2010 18:10:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Detecting Ajax Events on the Server</title>
		<link>http://www.learningjquery.com/2010/03/detecting-ajax-events-on-the-server</link>
		<comments>http://www.learningjquery.com/2010/03/detecting-ajax-events-on-the-server#comments</comments>
		<pubDate>Sat, 06 Mar 2010 17:20:50 +0000</pubDate>
		<dc:creator>Doug Neiner</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1015</guid>
		<description><![CDATA[When working with an Ajax-enhanced website, it's generally a good idea to provide a regular request fallback for any core functionality of the site. When you work to ensure that a fallback is in place, you will be faced with determining when a particular request is an Ajax request or just a normal page request. [...]]]></description>
			<content:encoded><![CDATA[<p>When working with an Ajax-enhanced website, it's generally a good idea to provide a regular request fallback for any core functionality of the site. When you work to ensure that a fallback is in place, you will be faced with determining when a particular request is an Ajax request or just a normal page request. In most situations, it's considered a best practice to <em>build your site without Ajax first</em>, adding the Ajax functionality afterward so the fallbacks are in place from the beginning. </p><p>Fortunately, jQuery makes it super easy to differentiate the Ajax requests from normal page views.</p>
<span id="more-1015"></span>

<h4 id="x_requested_with_header">X-Requested-With Header</h4>

<p>If you were to <a href="http://github.com/jquery/jquery/blob/master/src/ajax.js#L366">browse the jQuery source</a>, you would see that jQuery adds a special HTTP header to (almost) every Ajax request it makes with <code>$.ajax()</code>, <code>$.get()</code>, <code>$.getJSON()</code>, <code>$.post()</code> and <code>.load()</code> methods. </p>

<div class="igBar"><span id="ljavascript-8"><a href="#" onclick="javascript:showPlainTxt('javascript-8'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-8">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>remote <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;X-Requested-With&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;XMLHttpRequest&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />
<p>Keep in mind, though, that the header is not sent with <code>$.getScript()</code> or any JSONP request made with <code>$.ajax()</code> or <code>$.getJSON()</code>, since they aren't technically Ajax requests.</p>

<h4 id="detection_on_the_server">Detection on the Server</h4>

<p>By checking for this header, you can easily determine on the server if the request was initiated by jQuery. Each server-side language and web server combination will give you different ways of accessing this header, but let's look at a few.</p>

<p>In PHP, you can use this function to determine the type of request:</p>

<div class="igBar"><span id="lphp-9"><a href="#" onclick="javascript:showPlainTxt('php-9'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">PHP:</span><br /><div id="php-9">
<div class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#000000; font-weight:bold;">function</span> is_xhr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#616100;">return</span> <span style="color:#66CC66;">@</span> <span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#FF0000;">'HTTP_X_REQUESTED_WITH'</span> <span style="color:#006600; font-weight:bold;">&#93;</span> === <span style="color:#FF0000;">'XMLHttpRequest'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> is_xhr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#FF9933; font-style:italic;">// respond to Ajax request</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#FF9933; font-style:italic;">// respond to normal request</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p><a href="http://www.sinatrarb.com/">Sinatra</a> and <a href="http://rubyonrails.org">Ruby on Rails</a> have special shortcuts already built in:</p>

<div class="igBar"><span id="lruby-10"><a href="#" onclick="javascript:showPlainTxt('ruby-10'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">RUBY:</span><br /><div id="ruby-10">
<div class="ruby" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#9966CC; font-weight:bold;">if</span> request.<span style="color:#9900CC;">xhr</span>?</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#008000; font-style:italic;"># respond to Ajax request</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#9966CC; font-weight:bold;">else</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#008000; font-style:italic;"># respond to normal request</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#9966CC; font-weight:bold;">end</span></div></li>
</ol></div>
</div></div><br />

<p>Since other libraries outside of jQuery also set the <code>X-Requested-With</code> header, many server languages have special support for reading it. Check the documentation for your favorite server language to see if it has any convenience methods for detecting an Ajax request.</p>

<h4 id="a_small_example">A Small Example</h4>

<p>Ok, let's apply this knowledge to add Ajax to a simple email signup form. Here is our existing HTML:</p>

<div class="igBar"><span id="lhtml-11"><a href="#" onclick="javascript:showPlainTxt('html-11'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-11">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email_form&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;POST&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;p&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #000000;">&gt;</span></span>Email Address: <span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>label&gt;&lt;br <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/p&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;p&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Subscribe&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/p&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/form&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>And here is the existing <code>email.php</code> (<em>Note: on a normal error you might redirect back to show a message, or render the form again. This is for example only</em>):</p>

<div class="igBar"><span id="lphp-12"><a href="#" onclick="javascript:showPlainTxt('php-12'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">PHP:</span><br /><div id="php-12">
<div class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp;<span style="color:#0000FF;">$email</span> = <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#66CC66;">@</span><span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'email'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp;<span style="color:#FF9933; font-style:italic;">// Call our special email subscribe function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp;<span style="color:#0000FF;">$success</span> &nbsp;= subscribe<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$email</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#000000; font-weight:bold;">?&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;!DOCTYPE html&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;html&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &lt;head&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;&lt;title&gt;Email Signup&lt;/title&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;&lt;style type=&quot;text/css&quot;&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .success { color: green }</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .error &nbsp; { color: red }</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;&lt;/style&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &lt;/head&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &lt;body&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?php</span> <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$success</span> <span style="color:#006600; font-weight:bold;">&#41;</span>: <span style="color:#000000; font-weight:bold;">?&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &lt;div class=&quot;success&quot;&gt;Your Signup Is Successful! Thank you!&lt;/div&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?php</span> <span style="color:#616100;">else</span>: <span style="color:#000000; font-weight:bold;">?&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &lt;div class=&quot;error&quot;&gt;Something went wrong. </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Please click your browser's back button and try again.&lt;/div&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#000000; font-weight:bold;">&lt;?php</span> <span style="color:#616100;">endif</span>; <span style="color:#000000; font-weight:bold;">?&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &lt;/body&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&lt;/html&gt;</div></li>
</ol></div>
</div></div><br />

<p>The previous two pages work together to provide a simple signup functionality, without JS or Ajax involvement.</p>

<p>Now let's add the Ajax. We&#8217;ll add the following to the form page:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="">&lt;script</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="">&lt;script</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span><span style="">&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#email_form&quot;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div id='notice'&gt;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=submit"><span style="">submit</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Cancel the normal submission of the form</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> email <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#email&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=val"><span style="">val</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// perform validation; not shown</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Make our request and expect JSON in return</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.post"><span style="">$.<span style="color: #660066;">post</span></span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'email.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> email<span style="color: #339933;">:</span> email <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a>.<span style="color: #660066;">success</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#notice&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div id='success'&gt;Your Signup Is Successful! Thank you!&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#notice&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div id='error'&gt;There was an error. Please make sure your email is valid and try again.&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'json'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="">&lt;/script&gt;</span></div></li>
</ol></div>
</div></div><br />

<p>Finally, we replace the initial PHP code block in our <strong>email.php</strong> file to take advantage of the Ajax submission:</p>

<div class="igBar"><span id="lphp-14"><a href="#" onclick="javascript:showPlainTxt('php-14'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">PHP:</span><br /><div id="php-14">
<div class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#000000; font-weight:bold;">function</span> is_xhr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#616100;">return</span> <span style="color:#66CC66;">@</span> <span style="color:#0000FF;">$_SERVER</span><span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#FF0000;">'HTTP_X_REQUESTED_WITH'</span> <span style="color:#006600; font-weight:bold;">&#93;</span> === <span style="color:#FF0000;">'XMLHttpRequest'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#0000FF;">$email</span> = <a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$_POST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">'email'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#FF9933; font-style:italic;">// Call our special email subscribe function</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#0000FF;">$success</span> &nbsp;= subscribe<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$email</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> is_xhr<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// Not explicitly needed, but we like being accurate, right?:</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <a href="http://www.php.net/header"><span style="color:#000066;">header</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Content-type: application/json'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$success</span> <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> json_encode<span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">'success'</span> =<span style="color:#66CC66;">&gt;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> &nbsp;<span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> json_encode<span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/array"><span style="color:#000066;">array</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">'error'</span> =<span style="color:#66CC66;">&gt;</span> <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// We don't need to render anything else</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color:#FF9933; font-style:italic;">// ... the rest of our original PHP page</span></div></li>
</ol></div>
</div></div><br />

<h4 id="conclusion">Conclusion</h4>

<p>That's it! A few lines of code, and your server side code can now render different content depending on how the request was generated. Now that you know how to detect jQuery Ajax events, you no longer need to resort to passing <code>ajax: true</code> as <code>data</code> on your requests or calling a separate file altogether. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/03/detecting-ajax-events-on-the-server/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using Delegate and Undelegate in jQuery 1.4.2</title>
		<link>http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2</link>
		<comments>http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2#comments</comments>
		<pubDate>Fri, 05 Mar 2010 05:59:31 +0000</pubDate>
		<dc:creator>Jordan Boesch</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1004</guid>
		<description><![CDATA[As some of you have heard, there have been two new methods added in jQuery 1.4.2, .delegate() and .undelegate(). These methods achieve the same thing as the .live() and .die() methods, they just use a different syntax. For those new to .live(), it's a method in jQuery that allows you to attach events to elements [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you have heard, there have been two new methods added in jQuery 1.4.2, <a href="http://api.jquery.com/delegate/">.delegate()</a> and <a href="http://api.jquery.com/undelegate/">.undelegate()</a>. These methods achieve the same thing as the <a href="http://api.jquery.com/live/">.live()</a> and <a href="http://api.jquery.com/die/">.die()</a> methods, they just use a different syntax. For those new to <code>.live()</code>, it's a method in jQuery that allows you to attach events to elements that appear in the document as well as elements that will appear in the future. An example would be if you attached a click event via <code>.live()</code>:
</p>
<span id="more-1004"></span>
<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img.photo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; lightboxify<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Then appended some photos via ajax later on:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// append an image </span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;face.jpg&quot; alt=&quot;silly face&quot; class=&quot;photo&quot;/&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The click event would still apply to that new image without having to re-bind the event. Handy, isn't it?</p>
  <p>Not too long ago, the <code>.live()</code> method was brought up for discussion for a <a href="http://forum.jquery.com/topic/jquery-live-jquery-fn-live-discussion">few</a> <a href="http://paulirish.com/2010/on-jquery-live/">reasons</a>. One problem discussed is that <code>.live()</code> fails when you try to use it alongside traversals like:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// FAILS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// FAILS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=nextAll"><span style="">nextAll</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>and also when you pass any native DOM elements like:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// FAILS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Unfortunately, when you use <code>.live()</code>, it has to be at the top of the chain like so:</p>
<div class="igBar"><span id="ljavascript-31"><a href="#" onclick="javascript:showPlainTxt('javascript-31'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-31">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// WORKS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
</ol></div>
</div></div><br />
<p>Because this can be frustrating and confusing for many users who are used to the traversing and chainability that jQuery offers, it sparked a discussion about the syntax for <code>.live()</code>. Why does it look like all the other methods, yet does not behave the same?  Since changing the syntax would result in a whirlwind of code breakage, the jQuery team decided to introduce <code>.delegate()</code> and <code>.undelegate()</code> to complement <code>.live()</code> and <code>.die()</code>. Here's an example of how you would normally use <code>.live()</code> and <code>.die()</code> and how you can now use <code>.delegate()</code> and <code>.undelegate()</code>:</p>

<h4>Old way</h4>
<div class="igBar"><span id="ljavascript-32"><a href="#" onclick="javascript:showPlainTxt('javascript-32'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-32">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// Using .live()</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hover&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hover&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// Using .die()</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">die</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hover&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>New way</h4>
<div class="igBar"><span id="ljavascript-33"><a href="#" onclick="javascript:showPlainTxt('javascript-33'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-33">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// Using .delegate()</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;hover&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hover&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// Using .undelegate()</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">undelegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;hover&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The benefit of <code>delegate()</code> is that it allows you to specify its context. This way, it ensures that we do not bubble all the way up the DOM tree to capture the target of the element. With the <code>.live()</code> method, it bubbles all the way up the DOM each time unless you set context like so: <code>$('td', $('table')[0]).live('hover', function(){})</code>. That just looks ugly.
</p>
<p>Some often like to think of <code>delegate()</code> like a <code>bind()</code> call.  The syntax is a little different as you can see below.
<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// .bind() way</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul li'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #006600; font-style: italic;">// Do something with bind</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// .delegate() way</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #006600; font-style: italic;">// Do something with delegate</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br /> 
</p><p>In short, the difference between <code>.bind()</code> and <code>.delegate()</code> is that <code>.bind()</code> will only add events to the elements that are on the page when you call it. <code>.delegate()</code> is listening for new elements and then adding events to them when they appear on the page.</p>
  
<h4>The gotchas of delegate</h4>
<p>While it does behave like <code>.bind()</code>, it does <strong>not</strong> allow you to pass an object map of events like <code>.bind()</code> does. Take this <code>.bind()</code> method for example:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// This works wonderfully</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul li'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Something on click</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=mouseover"><span style="">mouseover</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Something on mouse over</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br /> 
<p>An error will be thrown when you try to do:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// FAILS!</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Something on click</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=mouseover"><span style="">mouseover</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Something on mouse over</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>I'm not sure the reasoning behind not implementing this, but I guess <a href="http://forum.jquery.com/topic/live-events-and-delegate-selector-events">I'm not the only one</a> pondering it.</p>

<p>Granted, <code>.bind()</code> didn't have this feature until jQuery 1.4. But if you'd like this same feature in .<code>live()</code> and <code>.delegate()</code>, Robert Katic wrote a small piece of code that you can include. <a href="http://gist.github.com/310747">Grab the gist here</a>.</p>

<p>I recommend using Robert Katic's patch above, but of course there are other approaches people can take. For example, you can rig up your own custom object map:</p>
<div class="igBar"><span id="ljavascript-37"><a href="#" onclick="javascript:showPlainTxt('javascript-37'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-37">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> customObjMap <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Something on click</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=mouseover"><span style="">mouseover</span></a> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Something on mouse over</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ol'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'click mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.isFunction"><span style="">$.<span style="color: #660066;">isFunction</span></span></a><span style="color: #009900;">&#40;</span>customObjMap<span style="color: #009900;">&#91;</span>e.<span style="color: #660066;">type</span><span style="color: #009900;">&#93;</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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; customObjMap<span style="color: #009900;">&#91;</span>e.<span style="color: #660066;">type</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> e<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Another "gotcha" with both <code>.delegate()</code> and <code>.live()</code> is that when you add the events <code>mouseenter</code> and <code>mouseleave</code> to an element, and then check the event type (<code>e.type</code>) in the callback function, it incorrectly displays as <code>mouseover</code> and <code>mouseout</code>. Using <code>.bind()</code>, on the other hand, it displays as <code>mouseenter</code> and <code>mouseleave</code> as expected. Here is an example:

</p>
<div class="igBar"><span id="ljavascript-38"><a href="#" onclick="javascript:showPlainTxt('javascript-38'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-38">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ol'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'mouseenter'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// outputs mouseover</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ol li'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseenter'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// outputs mouseenter</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><strong>UPDATE:</strong> This has been <a href="http://github.com/jquery/jquery/commit/28b489bfc8ad240b01e63ed93634f2b8770332a3">fixed</a> and will be rolled out in the next version of jQuery.</p>

<p>Overall, the "gothcas" are no match for the benefits that <code>.delegate()</code> and <code>.undelegate()</code> provide. Truly great additions to the jQuery core.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2/feed</wfw:commentRss>
		<slash:comments>12</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. 
  [...]]]></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-50"><a href="#" onclick="javascript:showPlainTxt('html-50'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-50">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-51"><a href="#" onclick="javascript:showPlainTxt('javascript-51'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-51">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span>.<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-52"><a href="#" onclick="javascript:showPlainTxt('javascript-52'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-52">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span>.<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-53"><a href="#" onclick="javascript:showPlainTxt('javascript-53'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-53">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span>.<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span>.<span style="color: #660066;">className</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-54"><a href="#" onclick="javascript:showPlainTxt('html-54'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-54">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-55"><a href="#" onclick="javascript:showPlainTxt('javascript-55'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-55">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> event.<span style="color: #660066;">target</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span>.<span style="color: #660066;">className</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-56"><a href="#" onclick="javascript:showPlainTxt('javascript-56'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-56">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> $elem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$elem.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>$elem.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-57"><a href="#" onclick="javascript:showPlainTxt('javascript-57'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-57">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-58"><a href="#" onclick="javascript:showPlainTxt('javascript-58'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-58">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">currentTarget</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-59"><a href="#" onclick="javascript:showPlainTxt('javascript-59'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-59">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-60"><a href="#" onclick="javascript:showPlainTxt('javascript-60'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-60">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<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>.<span style="color: #660066;">live</span><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#display&quot;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">currentTarget</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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>21</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-67"><a href="#" onclick="javascript:showPlainTxt('css-67'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">CSS:</span><br /><div id="css-67">
<div class="css" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808080; font-style: italic;">/* nav wrapper */</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808080; font-style: italic;">/* nav */</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.tab-nav </span>ul <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.tab-nav </span>a <span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000000;">width</span><span style="color: #66cc66;">:</span> 74px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000000;">height</span><span style="color: #66cc66;">:</span> 25px<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.tab-nav </span>ul ul <span style="color: #66cc66;">&#123;</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$topLinks1.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; direction;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$topLinks1.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; direction;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $topLinks1.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; direction;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $parentItem</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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-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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $topLinks2.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; direction;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $topLinks2.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+='</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $parentItem</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $topLinks3.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; direction;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $topLinks3.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; direction <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-='</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $parentItem</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; .<span style="color: #660066;">mouseleave</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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $topLinks3.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; .<span style="color: #660066;">mouseenter</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; clearTimeout<span style="color: #009900;">&#40;</span>closeAll<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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>52</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[Tools]]></category>
		<category><![CDATA[jQuery Resources]]></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 [...]]]></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://www.learningjquery.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-74"><a href="#" onclick="javascript:showPlainTxt('asp-74'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">ASP:</span><br /><div id="asp-74">
<div class="asp" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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://www.learningjquery.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://www.learningjquery.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>30</slash:comments>
		</item>
		<item>
		<title>Shorthand methods for unbind</title>
		<link>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind</link>
		<comments>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind#comments</comments>
		<pubDate>Tue, 30 Jun 2009 15:32:22 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=798</guid>
		<description><![CDATA[Someone sent me an email the other day, asking that I add shorthand methods for .unbind(eventType) to the jQuery core file. He argued that since jQuery provides shorthands such as .click() for .bind('click'), it should also include .unclick() for .unbind('click') for consistency. But he didn't consider two things:

	I can't change jQuery's API.
	Those shorthand methods used [...]]]></description>
			<content:encoded><![CDATA[<p>Someone sent me an email the other day, asking that I add shorthand methods for <code>.unbind(eventType)</code> to the jQuery core file. He argued that since jQuery provides shorthands such as <code>.click()</code> for <code>.bind('click')</code>, it should also include <code>.unclick()</code> for <code>.unbind('click')</code> for consistency. But he didn't consider two things:</p>
<ol>
	<li>I can't change jQuery's API.</li>
	<li>Those shorthand methods used to be part of jQuery core, but with the release of 1.0, John Resig cleaned up the API quite a bit, removing all of the <code>.un<em>Event</em>()</code> and <code>.one<em>Event</em>()</code> methods.</li>
</ol>
<p>While I understand the desire for simplicity in developers' code and consistency in jQuery's API, I think Mr. Resig made the right decision removing the shorthand methods. Keeping both "un" and "one" shorthands in there would have meant an additional 44 methods, with very little gain for most users of the library. Nevertheless, it's fairly trivial to create a plugin for this sort of thing. </p>
<span id="more-798"></span>
<p>If we want the shorthand methods for a particular project, we can simply iterate through an array of event types and add the "un" or "one" method to the jQuery prototype (<code>jQuery.fn</code>). Here is what it might look like:</p>
[inline][/inline]

<div class="igBar"><span id="ljavascript-76"><a href="#" onclick="javascript:showPlainTxt('javascript-76'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-76">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> eventTypes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'resize'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'scroll'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'dblclick'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'mousedown'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mouseup'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mouseenter'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mouseleave'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'select'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'submit'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'keydown'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'keypress'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'keyup'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'error'</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span>eventTypes<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=index"><span style="">index</span></a><span style="color: #339933;">,</span> eventType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; jQuery.<span style="color: #660066;">fn</span><span style="color: #009900;">&#91;</span> <span style="color: #3366CC;">'un'</span> <span style="color: #339933;">+</span> eventType <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> fn <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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=unbind"><span style="">unbind</span></a><span style="color: #009900;">&#40;</span> eventType<span style="color: #339933;">,</span> fn <span style="color: #009900;">&#41;</span> ;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>To add the "one" shorthand methods, we can repeat lines 8&ndash; 9, using <code>one</code> in place of both <code>un</code> in line 8 and <code>unbind</code> in line 9.</p>
<div class="update">
<h4>Update</h4>
<p>My apologies to everyone who saw this article as it was originally written. I messed things up when I replaced jQuery's <code>$.each()</code> method with a native <code>for</code> loop and failed to test the revision. I've modified it once more, putting the <code>$.each()</code> method back in there. If you look at the jQuery core file where it creates <code>.click()</code> and friends, you'll see where I got the idea.</p>
<p>Many thanks to Collin, Ralf, and especially Matthew, who were very kind to post comments pointing out the problems with the original script. I put all three versions &mdash; the original, the modified one above, and Matthew's version &mdash; in a <a href="http://www.learningjquery.com/js/unevents.js">single file</a> for your downloading pleasure.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Quick Tip: Outline Elements on Hover</title>
		<link>http://www.learningjquery.com/2009/06/quick-tip-outline-elements-on-hover</link>
		<comments>http://www.learningjquery.com/2009/06/quick-tip-outline-elements-on-hover#comments</comments>
		<pubDate>Tue, 16 Jun 2009 20:05:37 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=780</guid>
		<description><![CDATA[Someone on the jQuery Google Group yesterday asked about how to display a border around elements on hover. Here is a quick script I wrote to achieve that effect:

[inline][/inline]
PLAIN TEXTJavaScript:
$&#40;document&#41;.ready&#40;function&#40;&#41; &#123;
&#160; $&#40;'.entrytext'&#41;
&#160; &#160; .mouseover&#40;function&#40;event&#41; &#123;
&#160; &#160; &#160; $&#40;event.target&#41;.addClass&#40;'outline-element'&#41;;
&#160; &#160; &#125;&#41;
&#160; &#160; .mouseout&#40;function&#40;event&#41; &#123;
&#160; &#160; &#160; $&#40;event.target&#41;.removeClass&#40;'outline-element'&#41;;
&#160; &#160; &#125;&#41;
&#160; &#160; .click&#40;function&#40;event&#41; &#123;
&#160; &#160; &#160; $&#40;event.target&#41;.toggleClass&#40;'outline-element-clicked'&#41;;
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Someone on the jQuery Google Group yesterday asked about how to display a border around elements on hover. Here is a quick script I wrote to achieve that effect:</p>
<span id="more-780"></span>
[inline][/inline]
<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.entrytext'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=mouseover"><span style="">mouseover</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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'outline-element'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=mouseout"><span style="">mouseout</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=removeClass"><span style="">removeClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'outline-element'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'outline-element-clicked'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The script will respond to mouseover, mouseout, and click within <code>&lt;div class="entrytext"&gt;</code>. I use mouseover/mouseout rather than mouseenter/mouseleave because the script relies on event bubbling. The <code>$(event.target)</code> selector ensures that the class manipulation occurs on the innermost element. You can try it out on this page by hovering over elements within the main content area.</p>

<p>The two classes that are being toggled have the following style rules:</p>

<div class="igBar"><span id="lcss-82"><a href="#" onclick="javascript:showPlainTxt('css-82'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">CSS:</span><br /><div id="css-82">
<div class="css" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.outlineElement </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000000;">outline</span><span style="color: #66cc66;">:</span> 1px <span style="color: #993333;">solid</span> #c00<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.outlineElementClicked </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000000;">outline</span><span style="color: #66cc66;">:</span> 1px <span style="color: #993333;">solid</span> #0c0<span style="color: #66cc66;">;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>The outline property is really handy for this sort of thing because it doesn't affect the element's dimensions or layout. Unfortunately, it isn't supported in Internet Explorer 6 or 7, so if you want to support them, you might want to use <code>border</code> or <code>background-color</code> instead in a separate IE-only stylesheet. Or, maybe IE has some proprietary property? Anyone know?</p>

<p>I'm not sure what the person wanted to do with this script. By itself, it doesn't seem particularly useful to me. Still, I think there are a few concepts in there that can be applied elsewhere, and maybe the script can be expanded to do something interesting.</p>

<div class="update">
  <h4>Update</h4>
  <p>After thinking about this for a night, I realized that the script could be written more succinctly by combining the <code>mouseover</code> and <code>mouseout</code> event types in a single <code>.bind()</code> method and toggling the class therein &hellip; </p>
</div>
<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.entrytext'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover 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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $tgt <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$tgt.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.syntax_hilite'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $tgt.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'outline-element'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'outline-element-clicked'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Notice that the updated script also takes advantage of the <code>.closest()</code> method, which was introduced in jQuery 1.3. I don't want to apply the outline to any of the syntax-highlighted code in the entry, so I make sure that neither the event target nor any of its ancestors has a class of "syntax_hilite." Remember, to test for the presence (or absence) of a selector, we need to include more than just the selector itself. Here I test for "not a <code>length</code>." What I'm actually looking for is a length of 0, but since 0 is "falsey" the ! operator works just fine here.</p>

<h4>One More Tip</h4>
<p>Come to think of it, I can make the script shorter still by including the <code>click</code> event type along with <code>mouseover</code> and <code>mouseout</code>. Then, inside the the handler, the class that is toggled is determined by <code>event.type</code>.  </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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.entrytext'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover mouseout click'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $tgt <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$tgt.<span style="color: #660066;">closest</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.syntax_hilite'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $tgt.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'click'</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'outline-element-clicked'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'outline-element'</span><span style="color: #009900;">&#41;</span>; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Sometimes when code becomes shorter, it can also get less readable and more difficult to modify later on. I'm not suggesting that the final code snippet is necessarily the <em>best</em> way to achieve the outcome. A lot depends on what else you're planning to do and how a certain piece fits into the project as a whole.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/06/quick-tip-outline-elements-on-hover/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Better, Stronger, Safer jQuerify Bookmarklet</title>
		<link>http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet</link>
		<comments>http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet#comments</comments>
		<pubDate>Wed, 29 Apr 2009 14:12:43 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[bookmarklets]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=687</guid>
		<description><![CDATA[A long time ago I built myself a little bookmarklet to load jQuery on pages that don't already have it. The idea was to allow me to play around with any page on the web, using jQuery in the Firebug (and now Safari or IE8) console. I blogged about it, got lots of great feedback, [...]]]></description>
			<content:encoded><![CDATA[<p>A long time ago I built myself a little bookmarklet to load jQuery on pages that don't already have it. The idea was to allow me to play around with any page on the web, using jQuery in the Firebug (and now Safari or IE8) console. I <a href="http://www.learningjquery.com/2006/12/jquerify-bookmarklet">blogged about it</a>, got lots of great feedback, and then <a href="http://www.learningjquery.com/2008/06/updated-jquery-bookmarklet">blogged about an improved version</a>. Now that a lot more great feedback has come through the comments of the updated bookmarklet post, I've decided to update it one more time.</p>
<span id="more-687"></span>
<h4>The Bookmarklet</h4>
<p>To use the bookmarklet, drag the following link to your bookmark/favorites list:</p>
<p> 
&raquo; [inline]<a href="javascript:%20(function(){var%20el=document.createElement('div'),b=document.getElementsByTagName('body')[0];otherlib=false,msg='';el.style.position='fixed';el.style.height='32px';el.style.width='220px';el.style.marginLeft='-110px';el.style.top='0';el.style.left='50%';el.style.padding='5px%2010px%205px%2010px';el.style.zIndex=1001;el.style.fontSize='12px';el.style.color='#222';el.style.backgroundColor='#f99';if(typeof%20jQuery!='undefined'){msg='This%20page%20already%20using%20jQuery%20v'+jQuery.fn.jquery;return%20showMsg();}else%20if(typeof%20$=='function'){otherlib=true;}%20function%20getScript(url,success){var%20script=document.createElement('script');script.src=url;var%20head=document.getElementsByTagName('head')[0],done=false;script.onload=script.onreadystatechange=function(){if(!done&#038;&(!this.readyState||this.readyState=='loaded'||this.readyState=='complete')){done=true;success();}};head.appendChild(script);}%20getScript('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',function(){if(typeof%20jQuery=='undefined'){msg='Sorry,%20but%20jQuery%20wasn\'t%20able%20to%20load';}else{msg='This%20page%20is%20now%20jQuerified%20with%20v'+jQuery.fn.jquery;if(otherlib){msg+='%20and%20noConflict().%20Use%20$jq(),%20not%20$().';}}%20return%20showMsg();});function%20showMsg(){el.innerHTML=msg;b.appendChild(el);window.setTimeout(function(){if(typeof%20jQuery=='undefined'){b.removeChild(el);}else{jQuery(el).fadeOut('slow',function(){jQuery(this).remove();});if(otherlib){$jq=jQuery.noConflict();}}},2500);}})();">jQuerify</a>[/inline] &laquo;
</p>
<p>Then, when you're on a page in which you want to play around with jQuery in the console, just click the bookmarklet.</p>

<h4>Problems with the Other One</h4>
<p>The biggest problem with the former version was that it didn't work when other libraries that use the <code>$</code> function, such as Prototype and Mootools, were already loaded on the page. A number of people in the comments suggested that I simply add a <code>jQuery.noConflict();</code> line to the script. But I wasn't crazy about the idea of having to use <code>jQuery()</code> instead of <code>$()</code> even if no other libraries had been loaded. </p>

<p>Another problem, one that I noticed while testing my new version, was that sometimes the jQuery file that I was inserting from the Google CDN wouldn't fully load by the time I attempted to use it later in the bookmarklet. At least, I think that's what was going on. In any case, it was throwing a "jQuery is not defined" error.</p>

<h4>A Few Improvements in This One</h4>
<p>To handle the problems, I'm having the script do a few things differently:</p>
<ul>
  <li>Set up a counter to check 10 times if jQuery is loaded before giving up. </li>
  <li>Use a setTimeout to put a one-quarter-second (250ms) delay interval in between attempts.</li>
  <li>Check if the <code>$()</code> function is already being used by another library. If it is:
    <ul>
      <li>use jQuery's $.noConflict function to release the <code>$()</code> to the other library. </li>
      <li>set <code>$jq</code> as an alias to <code>jQuery</code> for convenience.</li>
      <li>mention in the flash notice that jQuery is in noConflict mode and that <code>$jq()</code> should be used instead of <code>$()</code>.</li>
    </ul>
  </li>
  <li>If after multiple attempts jQuery still won't load for some reason, set the flash notice's message accordingly and then remove it through plain old JavaScript rather than jQuery.</li>
</ul>

<p>Here is the script in its unbookmarkleted form:</p>

<div class="igBar"><span id="ljavascript-87"><a href="#" onclick="javascript:showPlainTxt('javascript-87'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-87">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> s<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; el<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; b<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</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>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> otherlib<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; startCounter<span style="color: #339933;">=</span>tryCounter<span style="color: #339933;">=</span><span style="color: #CC0000;color:#800000;">10</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; delay<span style="color: #339933;">=</span><span style="color: #CC0000;color:#800000;">250</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; s.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=position"><span style="">position</span></a><span style="color: #339933;">=</span><span style="color: #3366CC;">'fixed'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=height"><span style="">height</span></a><span style="color: #339933;">=</span><span style="color: #3366CC;">'32px'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=width"><span style="">width</span></a><span style="color: #339933;">=</span><span style="color: #3366CC;">'220px'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">marginLeft</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'-110px'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'0'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'50%'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">padding</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'5px 10px 5px 10px'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">fontSize</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'12px'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#222'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#f99'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery<span style="color: #339933;">!=</span><span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">'This page already using jQuery v'</span><span style="color: #339933;">+</span>jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jquery</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> showMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $<span style="color: #339933;">==</span><span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; otherlib<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'head'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> showMsg<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; el.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>msg;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; b.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; 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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery<span style="color: #339933;">==</span><span style="color: #3366CC;">'undefined'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=fadeOut"><span style="">fadeOut</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=remove"><span style="">remove</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>otherlib<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $jq<span style="color: #339933;">=</span>jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2500</span><span style="color: #009900;">&#41;</span>; &nbsp; &nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> tryjQuery<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; 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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery<span style="color: #339933;">==</span><span style="color: #3366CC;">'undefined'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tryCounter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tryCounter<span style="color: #339933;">--</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tryjQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">'Sorry, but after '</span> <span style="color: #339933;">+</span> startCounter <span style="color: #339933;">+</span> <span style="color: #3366CC;">' attempts, jQuery hasn'</span>t loaded<span style="color: #3366CC;">';</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showMsg();</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp;} else {</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp; &nbsp;msg='</span><span style="color: #000066; font-weight: bold;">This</span> page <span style="color: #000066; font-weight: bold;">is</span> now jQuerified <span style="color: #000066; font-weight: bold;">with</span> v<span style="color: #3366CC;">' + jQuery.fn.jquery;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp; &nbsp;if (otherlib) {msg+='</span> and noConflict<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>. <span style="color: #003366; font-weight: bold;">Use</span> $jq<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=not"><span style="">not</span></a> $<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #3366CC;">';}</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp; &nbsp;showMsg();</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp; &nbsp;}</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp; &nbsp;}, delay);</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp;};</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;"> &nbsp;tryjQuery();</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #3366CC;">})();</span></div></li>
</ol></div>
</div></div><br />
<p> I'm sure it can be improved even further. If you have suggestions for making it more elegant, more efficient, more betterer, I would love to hear them in the comments.</p>
<div class="update">
<h4>Update</h4>
<p>Based on a few comments, I've updated the script one more time to "basically watch the onload and readyState, and fire the callback, thus removing the necessity to poll," as <a href="#comment-72913">Paul Irish describes below</a>.</p>
<p>Here is the updated link (I also updated it above):</p>
<p>&raquo; [inline]<a href="javascript:%20(function(){var%20el=document.createElement('div'),b=document.getElementsByTagName('body')[0];otherlib=false,msg='';el.style.position='fixed';el.style.height='32px';el.style.width='220px';el.style.marginLeft='-110px';el.style.top='0';el.style.left='50%';el.style.padding='5px%2010px%205px%2010px';el.style.zIndex=1001;el.style.fontSize='12px';el.style.color='#222';el.style.backgroundColor='#f99';if(typeof%20jQuery!='undefined'){msg='This%20page%20already%20using%20jQuery%20v'+jQuery.fn.jquery;return%20showMsg();}else%20if(typeof%20$=='function'){otherlib=true;}%20function%20getScript(url,success){var%20script=document.createElement('script');script.src=url;var%20head=document.getElementsByTagName('head')[0],done=false;script.onload=script.onreadystatechange=function(){if(!done&#038;&(!this.readyState||this.readyState=='loaded'||this.readyState=='complete')){done=true;success();}};head.appendChild(script);}%20getScript('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',function(){if(typeof%20jQuery=='undefined'){msg='Sorry,%20but%20jQuery%20wasn\'t%20able%20to%20load';}else{msg='This%20page%20is%20now%20jQuerified%20with%20v'+jQuery.fn.jquery;if(otherlib){msg+='%20and%20noConflict().%20Use%20$jq(),%20not%20$().';}}%20return%20showMsg();});function%20showMsg(){el.innerHTML=msg;b.appendChild(el);window.setTimeout(function(){if(typeof%20jQuery=='undefined'){b.removeChild(el);}else{jQuery(el).fadeOut('slow',function(){jQuery(this).remove();});if(otherlib){$jq=jQuery.noConflict();}}},2500);}})();">jQuerify</a>[/inline] &laquo;</p>
<p>And here is the updated script:</p>
</div>
<div class="igBar"><span id="ljavascript-88"><a href="#" onclick="javascript:showPlainTxt('javascript-88'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-88">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> el<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; b<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</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>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; otherlib<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=position"><span style="">position</span></a><span style="color: #339933;">=</span><span style="color: #3366CC;">'fixed'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=height"><span style="">height</span></a><span style="color: #339933;">=</span><span style="color: #3366CC;">'32px'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=width"><span style="">width</span></a><span style="color: #339933;">=</span><span style="color: #3366CC;">'220px'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">marginLeft</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'-110px'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'0'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'50%'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">padding</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'5px 10px 5px 10px'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">zIndex</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">1001</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">fontSize</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'12px'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">color</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#222'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; el.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#f99'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery<span style="color: #339933;">!=</span><span style="color: #3366CC;">'undefined'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">'This page already using jQuery v'</span><span style="color: #339933;">+</span>jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jquery</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> showMsg<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> $<span style="color: #339933;">==</span><span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; otherlib<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #006600; font-style: italic;">// more or less stolen form jquery core and adapted by paul irish</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> getScript<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span>success<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> script<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; script.<span style="color: #660066;">src</span><span style="color: #339933;">=</span>url;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> head<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'head'</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: #339933;">,</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; done<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Attach handlers for all browsers</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; script.<span style="color: #000066;">onload</span><span style="color: #339933;">=</span>script.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>done &#038;& <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'loaded'</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'complete'</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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; done<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; success<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; head.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>script<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; getScript<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery<span style="color: #339933;">==</span><span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">'Sorry, but jQuery wasn<span style="color: #000099; font-weight: bold;">\'</span>t able to load'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; msg<span style="color: #339933;">=</span><span style="color: #3366CC;">'This page is now jQuerified with v'</span> <span style="color: #339933;">+</span> jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jquery</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>otherlib<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>msg<span style="color: #339933;">+=</span><span style="color: #3366CC;">' and noConflict(). Use $jq(), not $().'</span>;<span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> showMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> showMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; el.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>msg;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; b.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; 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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery<span style="color: #339933;">==</span><span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; b.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=fadeOut"><span style="">fadeOut</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=remove"><span style="">remove</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>otherlib<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $jq<span style="color: #339933;">=</span>jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2500</span><span style="color: #009900;">&#41;</span>; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Let me know if this one causes any problems.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet/feed</wfw:commentRss>
		<slash:comments>53</slash:comments>
		</item>
		<item>
		<title>3 Quick Steps for a Painless Upgrade to jQuery 1.3</title>
		<link>http://www.learningjquery.com/2009/03/3-quick-steps-for-a-painless-upgrade-to-jquery-13</link>
		<comments>http://www.learningjquery.com/2009/03/3-quick-steps-for-a-painless-upgrade-to-jquery-13#comments</comments>
		<pubDate>Fri, 06 Mar 2009 05:58:09 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[selectors]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=577</guid>
		<description><![CDATA[Since the release of jQuery 1.3 a month and a half ago, I've been keeping my eye on any troubles that people have had with their upgrades. Fortunately, most people have had no problems at all. For those who have, the issues have almost always been in one of three areas. Identifying these areas and [...]]]></description>
			<content:encoded><![CDATA[<p>Since the release of jQuery 1.3 a month and a half ago, I've been keeping my eye on any troubles that people have had with their upgrades. Fortunately, most people have had no problems at all. For those who have, the issues have almost always been in one of three areas. Identifying these areas and adjusting any legacy scripts ahead of time will go a long way toward ensuring a smooth transition to jQuery 1.3.x.</p>
<span id="more-577"></span>
[inline][/inline]
<h4>1. Update Attribute Selectors</h4>
<p>By far the most common stumbling block to the 1.3 upgrade has been the attribute selector. The XPath syntax for this selector &mdash; <code>[@attribute]</code> &mdash; has been <a href="http://blog.jquery.com/2007/08/24/jquery-114-faster-more-tests-ready-for-12/">deprecated since version 1.1.4</a> in August, 2007.  Still, many scripts, including some prominent plugins, continued to use the old syntax, which wasn't a problem until jQuery 1.3, at which point it was no longer supported. The correct syntax (since 1.1.4) follows the CSS standard: <code>[attribute]</code>.</p>
<p>The fix is quite simple. Simply search your scripts for <code>[@</code>. Go through the matches one by one, and if the string is being used as an attribute selector (and not, for example, in a regular expression), replace it with <code>[</code>. 
<h5>Removed XPath Syntax</h5>
</p><p>This will break your code in jQuery 1.3+:</p>

<div class="igBar"><span id="ljavascript-95"><a href="#" onclick="javascript:showPlainTxt('javascript-95'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-95">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[@href^=http]'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[@title=foo]'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />   

<h5>Current CSS Syntax</h5>  
<p>Use this instead:</p>
<div class="igBar"><span id="ljavascript-96"><a href="#" onclick="javascript:showPlainTxt('javascript-96'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-96">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href^=http]'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[title=foo]'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>2. Check Custom Selectors</h4>
<p>If you are using custom selectors, either in your own script or within a plugin, you might run across an error if the second argument's object value is a string: </p>

<div class="igBar"><span id="ljavascript-97"><a href="#" onclick="javascript:showPlainTxt('javascript-97'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-97">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">jQuery.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=extend"><span style="">extend</span></a><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; hasSiblings<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jQuery(a).siblings(m[3]).length&gt;0&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This is another easy fix. Simply change that value to an anonymous function and add what was in the string to the <code>return</code> statement:</p>
<div class="igBar"><span id="ljavascript-98"><a href="#" onclick="javascript:showPlainTxt('javascript-98'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-98">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">jQuery.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=extend"><span style="">extend</span></a><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; hasSiblings<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>i<span style="color: #339933;">,</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> jQuery<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=siblings"><span style="">siblings</span></a><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;color:#800000;">0</span>;<span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>So, basically, you just change <code>"jQuery(a).siblings(m[3]).length>0"</code> to <code>function(a,i,m) {return <strong>jQuery(a).siblings(m[3]).length>0</strong>;}</code>.
</p>
<p>When someone on the <a href="http://groups.google.com/group/jquery-en/">jQuery Google Group</a> was having trouble with George Adamson's excellent <a href="http://www.softwareunity.com/jquery/JQueryMoreSelectors/">More Selectors plugin</a>, all I had to do to make it compatible with jQuery 1.3 was update the attribute selectors and change custom selectors (my updated script is <a href="http://plugins.learningjquery.com/moreselectors/">available here</a> temporarily until George has time to update the original).</p>
<h4>3. Note Visible and Hidden Elements</h4>
<p>This change didn't occur until jQuery 1.3.2, but if you are upgrading to the latest and greatest jQuery version, you'll want to be sure your scripts aren't relying on a quirk in how jQuery was determining the visibility of elements. Prior to 1.3.2, if you had an element with <code>display:block</code> inside a containing element with <code>display: none</code>, the inner element would still be reported as visible.
</p>

<div class="igBar"><span id="lhtml-99"><a href="#" onclick="javascript:showPlainTxt('html-99'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-99">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;outer1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inner1&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; you can't see me!</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Before version 1.3.2, <code>$('#inner'1).is(':visible')</code> would return <code>true</code> and <code>$('#inner1:hidden')</code> would match 0 elements. In order to find out if an element were truly hidden, meaning you couldn't see it, you'd have to check all of its ancestors to see if they were hidden as well. Remy Sharp put together a little custom selector to check if elements were <a href="remysharp.com/2008/10/17/jquery-really-visible/">*really* visible</a>.</p>

<p>With 1.3.2, <code>$('#inner').is(':visible)</code> returns <code>false</code> and <code>$('#inner:hidden')</code> match 1 element.</p>

<h5>Another Visible Wrinkle</h5>

<p>There is one more change in how visibility is detected in 1.3.2: elements with <code>visibility: hidden</code> are now considered visible, whereas before they were considered hidden.</p>

<div class="igBar"><span id="lhtml-100"><a href="#" onclick="javascript:showPlainTxt('html-100'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-100">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;outer2&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inner2&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;visibility: hidden&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;you can't see me!</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Given the above div structure, jQuery 1.3.2 considers #inner2 visible while prior versions considered it hidden.</p>
<p>Here is a quick demo, with two iFrames showing the differences in determining visibility:</p>

<iframe src="/examples/visibility-1.2.6.html" style="height:15.5em;width:260px;float:left; border-right: 1px solid #999; margin-right: 16px;"></iframe>
<iframe src="/examples/visibility-1.3.2.html" style="height:15.5em;width:300px;float:left"></iframe>

<p style="clear: left">I'm not sure how I feel about the new behavior with <code>visibility:hidden</code>, but I think the change was important for performance reasons.</p>
<h4>A Few Others</h4>
<p>There are a few other issues that may affect a small minority of users, but the three above represent the lion's share of reported problems that I've seen on the Google Groups. For more information about changes, see the <a href="http://docs.jquery.com/Release:jQuery_1.3#Upgrading">jQuery 1.3 Release Notes</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/03/3-quick-steps-for-a-painless-upgrade-to-jquery-13/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Slide Elements in Different Directions</title>
		<link>http://www.learningjquery.com/2009/02/slide-elements-in-different-directions</link>
		<comments>http://www.learningjquery.com/2009/02/slide-elements-in-different-directions#comments</comments>
		<pubDate>Fri, 06 Feb 2009 13:58:44 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[animations]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=348</guid>
		<description><![CDATA[Although jQuery has a nice set of slide methods &#8212; .slideDown(), .slideUp(), and .slideToggle() &#8212; sometimes we may want to slide an element in a different direction. Fortunately, it's pretty easy to do.

Reverse the Slide Direction
[inline][/inline]
With the built-in slide methods, elements are shown by sliding them down and into view. But what if we want [...]]]></description>
			<content:encoded><![CDATA[<p>Although jQuery has a nice set of slide methods &mdash; <code>.slideDown()</code>, <code>.slideUp()</code>, and <code>.slideToggle()</code> &mdash; sometimes we may want to slide an element in a different direction. Fortunately, it's pretty easy to do.</p>
<span id="more-348"></span>
<h4>Reverse the Slide Direction</h4>
[inline][/inline]
<p>With the built-in slide methods, elements are shown by sliding them down and into view. But what if we want to slide something from the bottom up and into view? The trick here is to use some judicious CSS. Let's start with a simple HTML structure:</p>
<div class="igBar"><span id="lhtml-107"><a href="#" onclick="javascript:showPlainTxt('html-107'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-107">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;slidebottom&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;slide&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;button&gt;</span></span>slide it<span style="color: #009900;"><span style="color: #000000;">&lt;/button&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inner&quot;</span><span style="color: #000000;">&gt;</span></span>Slide from bottom<span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />
<p>To get the inner div to slide up, we'll anchor its bottom edge to the bottom of the bottom of the nearest positioned ancestor (in this case, the #slidebottom div):</p>
<div class="igBar"><span id="lcss-108"><a href="#" onclick="javascript:showPlainTxt('css-108'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">CSS:</span><br /><div id="css-108">
<div class="css" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.slide </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #6666ff;">.slide </span><span style="color: #6666ff;">.inner </span><span style="color: #66cc66;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000000;">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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000000;">bottom</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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #66cc66;">&#125;</span></div></li>
</ol></div>
</div></div><br />
<p>Other properties such as width, padding, margin, and background-color have been set for these elements, but only the essential properties for modifying the slide behavior are shown above.</p>
<p><strong>Note</strong>: I'll be using the term <em>positioned</em> to refer to elements that have the CSS <code>position</code> property set to something other than "static." Both <code>div</code>s in this example are positioned &mdash; one absolutely and the other relatively.</p>
<p>Now, we can write the jQuery the same way we would with a traditional slide effect:</p>
<div class="igBar"><span id="ljavascript-109"><a href="#" onclick="javascript:showPlainTxt('javascript-109'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-109">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#slidebottom button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slideToggle"><span style="">slideToggle</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Try it out:</p>

<div id="slidebottom" class="slide">
  <button>slide it</button>
  <div class="inner">Slide from bottom</div>
</div>
<h4>Horizontal Slides</h4>
<h5>Animate Width</h5>
<p>We can also slide elements to the left and right. The simplest way is to animate the element's width property. </p>
<div class="igBar"><span id="ljavascript-110"><a href="#" onclick="javascript:showPlainTxt('javascript-110'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-110">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#slidewidth button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=width"><span style="">width</span></a><span style="color: #339933;">:</span> <span style="color: #3366CC;">'toggle'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>In this case it's not necessary for the sliding element to be positioned.</p>
<div id="slidewidth" class="slide">
  <button>slide it</button>
  <div class="inner">Animate this element's width</div>
</div>
<p>While animating the width is fine for what it is, I'm not crazy about how the text wraps as the width decreases. One way to avoid the wrapping is to add a CSS declaration such as <code>white-space: nowrap;</code>, but that would mess up the appearance of an element with a lot of text&mdash;one in which we would expect to see text wrapping when it is at full length.</p>

<h5>Animate Left</h5>
<p>Another way to avoid the text-wrap issue is to animate the element's <code>left</code> property. Here, it's important once again to make sure that the element is positioned. After all, we can't move an element if it's <em>static</em>.</p>
<p>With this animation, we need to calculate how far to move the element. The following code rests on two assumptions: (1) the sliding element has an outerWidth() that is equal to or greater than its parent element's width, and (2) the sliding element is initially set to <code>left: 0;</code>. You may have to adjust your code if either one of these assumptions doesn't hold in your situation.</p>
<div class="igBar"><span id="ljavascript-111"><a href="#" onclick="javascript:showPlainTxt('javascript-111'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-111">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#slideleft button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $lefty <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $lefty.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; left<span style="color: #339933;">:</span> parseInt<span style="color: #009900;">&#40;</span>$lefty.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'left'</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> <span style="color: #339933;">==</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #339933;">?</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">-</span>$lefty.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=outerWidth"><span style="">outerWidth</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #CC0000;color:#800000;">0</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Lines 5 through 7 use a "ternary" operator that basically says, "If the left css property equals 0, move the element to the left as many pixels as it is wide (including padding and border); if not, move it back to 0."</p>
<div id="slideleft" class="slide">
  <button>slide it</button>
  <div class="inner">Animate this element's left style property</div>
</div>
<p>Also, if we want the element to be hidden when it slides to the left, we need to add <code>overflow: hidden;</code> to its parent element.</p>
<h5>Animate Left Margin</h5>
<p>Finally, we can achieve the same effect as the left animation by animating the <code>marginLeft</code> property. In this case, we still need <code>overflow: hidden;</code> on the parent element, but the sliding element does not need to be positioned.</p>
<div class="igBar"><span id="ljavascript-112"><a href="#" onclick="javascript:showPlainTxt('javascript-112'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-112">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#slidemarginleft button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $marginLefty <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $marginLefty.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; marginLeft<span style="color: #339933;">:</span> parseInt<span style="color: #009900;">&#40;</span>$marginLefty.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #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> <span style="color: #339933;">==</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #339933;">?</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $marginLefty.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=outerWidth"><span style="">outerWidth</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #CC0000;color:#800000;">0</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>For the sake of variety, we're sliding this one to the right.</p>
<div id="slidemarginleft" class="slide">
  <button>slide it</button>
  <div class="inner">Animate this element's margin-left style property</div>
</div>
<p>With a couple little tweaks, these horizontal slides can be used for a horizontal accordion.</p>
<h4>Further Resources</h4>
<ul>
  <li>To learn about adding easing effects to your slides, check out Brandon Aaron's <a href="http://www.learningjquery.com/2009/02/quick-tip-add-easing-to-your-animations">Quick Tip: Add Easing to Your Animations</a>.</li>
  <li>To convert any of these slides into a plugin for convenient reuse, see <a href="http://www.learningjquery.com/2008/02/simple-effects-plugins">Simple Effects Plugins</a>.</li>
  <li>For a whole bunch of ready-made slides and other effects, take a look at <a href="http://ui.jquery.com/demos/effects_showhide/">jQuery UI</a>.</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/02/slide-elements-in-different-directions/feed</wfw:commentRss>
		<slash:comments>74</slash:comments>
		</item>
	</channel>
</rss>
