<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Learning jQuery</title>
	
	<link>http://www.learningjquery.com</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Thu, 26 Aug 2010 16:55:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LearningJquery" /><feedburner:info uri="learningjquery" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>LearningJquery</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Great Ways to Learn jQuery</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/Xl_4hpdiAig/great-ways-to-learn-jquery</link>
		<comments>http://www.learningjquery.com/2010/07/great-ways-to-learn-jquery#comments</comments>
		<pubDate>Wed, 07 Jul 2010 15:11:38 +0000</pubDate>
		<dc:creator>Marc Grabanski</dc:creator>
				<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://learningjquery.com/?p=1164</guid>
		<description>These jQuery resources will set you on the path towards mastering jQuery.
Written Articles

Getting Started with jQuery &amp;#8211; this is the official jQuery getting started guide.
jQuery for JavaScript Programmers &amp;#8211; Simon Willison (creator of django) gives you an introduction to jQuery for people who already understand JavaScript.
jQuery Crash Course &amp;#8211; Nathan Smith gives a quick introduction [...]</description>
			<content:encoded><![CDATA[<p>These jQuery resources will set you on the path towards mastering jQuery.</p>
<h4>Written Articles</h4>
<ul>
<li><a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery">Getting Started with jQuery</a> &#8211; this is the official jQuery getting started guide.</li>
<li><a href="http://simonwillison.net/2007/Aug/15/jquery/">jQuery for JavaScript Programmers</a> &#8211; Simon Willison (creator of django) gives you an introduction to jQuery for people who already understand JavaScript.</li>
<li><a href="http://www.digital-web.com/articles/jquery_crash_course/">jQuery Crash Course</a> &#8211; Nathan Smith gives a quick introduction to jQuery on Digital Web Magazine.</li>
<li><a href="http://www.west-wind.com/presentations/jquery/">Introduction to jQuery</a> &#8211; Rick Strahl, well-known for his work developing with Microsoft technologies, gives his introduction to jQuery with part two covering <a href="http://www.west-wind.com/presentations/jquery/jQueryPart2.aspx">using jQuery with ASP.NET.</a></li>
</ul>
<h4>E-Books</h4>
<ul>
<li><a href="http://jqfundamentals.com/book/book.html">jQuery Fundamentals</a> &#8211; open-source e-book written by Rebecca Murphey in collaboration with other well-known members of the jQuery community.</li>
<li><a href="http://jqueryenlightenment.com/">jQuery Enlightenment</a> &#8211; Cody Lindley&#8217;s e-book covers advanced topics on jQuery with links to working code examples in jsbin.</li>
</ul>
<p> <span id="more-1164"></span></p>
<h4>jQuery Books</h4>
<ul>
<li><a href="http://oreilly.com/catalog/9780596159788">jQuery Cookbook</a> &#8211; collaborative book by many jQuery team members with recipes on many jQuery topics.</li>
<li><a href="https://www.packtpub.com/learning-jquery-1.3/book?mid=220409c024ep">Learning jQuery 1.3</a> &#8211; walkthrough of learning the jQuery JavaScript library through hands-on examples and explanations. Authored by Karl Swedberg &amp; Jonathan Chaffer</li>
<li><a href="https://www.packtpub.com/jquery-1-4-reference-guide/book">jQuery Reference 1.4</a> &#8211; practical examples of the entire jQuery 1.4 API. Authored by Karl Swedberg &amp; Jonathan Chaffer</li>
<li><a href="http://www.manning.com/bibeault2/">jQuery in Action 2nd edition</a> &#8211; a fast-paced introduction to jQuery that will take your JavaScript programming to the next level. Authored by Bear Bibeault and Yehuda Katz.</li>
<li><a href="https://www.packtpub.com/user-interface-library-for-jquery-ui-1-7/book">jQuery UI 1.7</a> &#8211; Build highly interactive web applications with ready-to-use widgets from the jQuery User Interface library. Authored by Dan Wellman.</li>
</ul>
<h4>Slideshows</h4>
<ul>
<li>
<div>John Resig</div>
<div><a href="http://ejohn.org/apps/workshop/intro/">Introduction to jQuery</a></div>
<div><a href="http://ejohn.org/apps/workshop/adv-talk/">Things You Might Not Know About jQuery</a></div>
<p><a href="http://ejohn.org/apps/workshop/adv-talk/index2.html">Improve Your Web App with jQuery</a></li>
<li>
<div>Remy Sharp</div>
<div><a href="http://remysharp.com/downloads/j4d-2010.pdf" title="PDF">(PDF download) jQuery for Designers</a></div>
<div><a href="http://remysharp.com/downloads/jquery-2010.pdf" title="PDF">(PDF download) jQuery write a bit more, do lots more</a></div>
<p><a href="http://remysharp.com/talks/">More talks from Remy</a></li>
<li>
<div>Marc Grabanski</div>
<div style="width:610px" id="__ss_1254680"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/1Marc/jquery-essentials" title="jQuery Essentials">jQuery Essentials</a></strong><object id="__sse1254680" width="610" height="500"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-essentials-090406094627-phpapp01&#038;stripped_title=jquery-essentials" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse1254680" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-essentials-090406094627-phpapp01&#038;stripped_title=jquery-essentials" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="610" height="500"></embed></object></div>
</li>
<li>
<div>Simon Willison</div>
<div style="width:610px" id="__ss_88304"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/simon/jquery-in-15-minutes">jQuery in 15 minutes</a></strong><object id="__sse1254680" width="610" height="500"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-in-15-minutes1421&#038;stripped_title=jquery-in-15-minutes" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse88304" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=jquery-in-15-minutes1421&#038;stripped_title=jquery-in-15-minutes" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="610" height="500"></embed></object></div>
</li>
</ul>
<h4>Learning Screencasts</h4>
<ul>
<li><a href="http://net.tutsplus.com/articles/web-roundups/jquery-for-absolute-beginners-video-series/">jQuery for Absolute Beginners</a> &#8211; Net Tuts screencast series starts by downloading the library and eventually work our way up to creating an AJAX style-switcher.</li>
<li><a href="http://jqueryfordesigners.com/">jQuery for Designers Screencasts</a> &#8211; a myriad of screencast videos to teach you jQuery from a designer&#8217;s perspective.</li>
<li>
<div><a href="http://css-tricks.com/video-screencasts/20-introduction-to-jquery/">Intro to jQuery</a> &#8211; Chris Coyier from CSS Tricks gives you an introduction to jQuery.</div>
<p>  <a href="http://css-tricks.com/video-screencasts/35-intro-to-jquery-2/">Intro to jQuery Part 2</a></li>
<li><a href="http://store.lullabot.com/products/introduction-to-jquery">Lullbot Introduction to jQuery</a> &#8211; this video walks the viewer through the essential concepts and syntaxes needed to start writing simple-yet-powerful JavaScript using jQuery.</li>
<li><a href="http://marcgrabanski.com/articles/the-jquery-course-prerelease">The jQuery Course Prerelease</a> &#8211; Marc Grabanski&#8217;s project to document all he&#8217;s learned from four years of jQuery programming into one educational course.</li>
<li><a href="http://peepcode.com/products/jquery">Meet jQuery</a> &#8211; this Peepcode Screencast teaches you jQuery through building a simple time-tracking application.</li>
<li><a href="http://www.bennadel.com/blog/1492-An-Intensive-Exploration-Of-jQuery-With-Ben-Nadel-Video-Presentation-.htm">An Intensive Introduction to jQuery</a> &#8211; Ben Nadal takes a deep dive into jQuery.</li>
</ul>
<h4>Attend a training course</h4>
<ul>
<li><a href="http://ideafoundry.info/jquery">Idea Foundry jQuery Training</a> &#8211; The brains behind <a href="http://collectiveidea.com/">Collective Idea</a> host jQuery classes led by Karl Swedberg in Holland, Michigan.</li>
<li><a href="http://www.jquerybootcamp.com/">jQuery Boot Camp</a> &#8211; Ben Nadal&#8217;s training course in New York City.</li>
<li><a href="https://training.thoughtbot.com/">Thoughtbot Training</a> &#8211; look here for training on jQuery from time to time.</li>
</ul>
<h4>Hire a Corporate jQuery Trainer</h4>
<ul>
<li><a href="http://appendto.com/training">appendTo Training</a> &#8211; the jQuery company offers corporate training.</li>
<li><a href="http://www.rebeccamurphey.com/">Rebecca Murphey</a> &#8211; started the open-source e-book jQuery Fundementals and is a member of the yayQuery podcast.</li>
<li><a href="http://www.learningjquery.com/contact/">Karl Swedberg</a> &#8211; wrote the Learning jQuery books and runs this website.</li>
<li><a href="http://marcgrabanski.com">Marc Grabanski</a> &#8211; creator of the jQuery UI Datepicker project and blogger.</li>
<li><a href="http://orderedlist.com/hire-us/">Ordered List</a> &#8211; all around great guys, creators of the Harmony CMS, and active speakers and writers.</li>
</ul>
<h4>Conferences</h4>
<ul>
<li><a href="http://carsonified.com/online-conferences/jquery/">Carsonified Online jQuery Conference</a></li>
<li><a href="http://events.jquery.org">Official jQuery Conference</a> &#8211; hosted in Boston, San Francisco, and other locations in the future.</li>
<li><a href="http://www.environmentsforhumans.com/jquery_summit/">jQuery Summit</a> &#8211; online conference.</li>
</ul>
<h4>jQuery Meetups</h4>
<p>Another great way to learn more jQuery is to start or join a <a href="http://meetups.jquery.com">jQuery Meetup</a>. Check to see if there is a meetup near you, and if not, start one!</p>
<h4>Podcasts</h4>
<ul>
<li><a href="http://blog.jquery.com/">Official jQuery Pocast</a></li>
<li><a href="http://yayquery.com/">yayQuery video podcast</a></li>
</ul>
<h4>More Resources</h4>
<ul>
<li><a href="http://twitter.com/jquery">jQuery on Twitter</a></li>
<li><a href="http://wefollow.com/tag/jquery">Twitter Directory for Tag jQuery</a></li>
<li><a href="http://jquerylist.com">jQuery List</a></li>
<li><a href="http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/">10 Things I Learned from the jQuery Source</a></li>
</ul>
<h4>Other Blogs</h4>
<ul>
<li><a href="http://remysharp.com/tag/jquery/">Remy Sharp jQuery</a></li>
<li><a href="http://www.filamentgroup.com/lab">Filament Group</a></li>
<li><a href="http://blog.nemikor.com/">Nemikor</a> &#8211; Scott González</li>
<li><a href="http://benalman.com/">Ben Alman</a></li>
<li><a href="http://paulirish.com">Paul Irish</a></li>
<li><a href="http://marcgrabanski.com">Marc Grabanski</a></li>
<li><a href="http://alexsexton.com/">Alex Sexton</a></li>
<li><a href="http://james.padolsey.com/">James Padolsey</a></li>
<li><a href="http://msdn.microsoft.com/en-us/scriptjunkie/">Script Junkie</a></li>
<li><a href="http://www.nczonline.net/">NCZOnline</a> &#8211; Nicholas Zakas</li>
<li><a href="http://perfectionkills.com/">Perfection Kills</a> &#8211; Juriy Zaytsev (kangax)</li>
<li><a href="http://www.phpied.com/">Stoyan Stefanov</a></li>
<li><a href="http://stevesouders.com/">Steve Souders</a></li>
<li><a href="http://css-tricks.com/">CSS Tricks</a></li>
<li><a href="http://www.webdesignerwall.com/">Web Designer Wall</a></li>
<li><a href="http://www.jankoatwarpspeed.com/">Janko At Warp Speed</a></li>
<li><a href="http://www.marcofolio.net/">Marco Folio</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/LearningJquery/~4/Xl_4hpdiAig" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/07/great-ways-to-learn-jquery/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2010/07/great-ways-to-learn-jquery</feedburner:origLink></item>
		<item>
		<title>Autocomplete Migration Guide</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/iegANoMS6K0/autocomplete-migration-guide</link>
		<comments>http://www.learningjquery.com/2010/06/autocomplete-migration-guide#comments</comments>
		<pubDate>Wed, 23 Jun 2010 13:28:54 +0000</pubDate>
		<dc:creator>Jörn Zaefferer</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery UI]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1106</guid>
		<description>The jQuery Autocomplete plugin got a successor recently, the jQuery UI Autocomplete. In this guide we'll look at the old plugin API step-by-step, and how to migrate to the new API.

At first it may look like the new plugin supports barely any of the old options. We'll see how all the old options can be [...]</description>
			<content:encoded><![CDATA[<p>The <a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jQuery Autocomplete plugin</a> got a successor recently, the <a href="http://jqueryui.com/demos/autocomplete/">jQuery UI Autocomplete</a>. In this guide we'll look at the <a href="http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions">old plugin API</a> step-by-step, and how to migrate to the new API.</p>

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

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

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

<div class="igBar"><span id="ljavascript-3"><a href="#" onclick="javascript:showPlainTxt('javascript-3'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-3">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>And that becomes, easy enough:</p>

<div class="igBar"><span id="ljavascript-4"><a href="#" onclick="javascript:showPlainTxt('javascript-4'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-4">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; source<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#93;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The same applies if you provided a URL as the first argument, although there is a difference between the two plugins for remote data. The old plugin expected a special format with pipes to separate values and newlines to separate rows. That is gone for good, the Autocomplete widget now works with JSON by default. The simplest form is the same as in the example above, an array of string values.</p>

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

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

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

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

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

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

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

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

<div class="igBar"><span id="ljavascript-16"><a href="#" onclick="javascript:showPlainTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-16">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">combobox</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

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

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

<p>Let's break this down, piece by piece:</p>

<div class="igBar"><span id="ljavascript-18"><a href="#" onclick="javascript:showPlainTxt('javascript-18'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-18">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">widget</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui.combobox&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; _create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// all the code</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This defines a new widget, in the <code>ui</code> namespace (don't use this for your own widgets, it's reserved for jQuery UI widgets) and adds the only method, <code>_create</code>. This is the constructor method for jQuery UI widgets, and will be called only once. In versions prior to 1.8 it was called <code>_init</code>. The <code>_init</code> method still exists, but it is called each time you call <code>.combobox()</code> (with or without options). Keep in mind that our widget implementation is not complete, as it lacks the <code>destroy</code> method. It's just a demo.</p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<div class="igBar"><span id="ljavascript-24"><a href="#" onclick="javascript:showPlainTxt('javascript-24'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-24">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;&lt;button&gt;&nbsp;&lt;/button&gt;&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<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;tabIndex&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="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;title&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Show All Items&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span> input <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<span style="color: #660066;">button</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; icons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; primary<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ui-icon-triangle-1-s&quot;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<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;">&quot;ui-corner-all&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;ui-corner-right ui-button-icon&quot;</span> <span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// close if already visible</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;widget&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// pass empty string as value to search for, displaying all results</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; input.<span style="color: #660066;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; input.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Another element is created on-the-fly. It gets <code>tabIndex="-1"</code> to take it out of the tab order, as it's mostly useful for mouse interactions. Keyboard interaction is already covered by the <code>input</code> element. It gets a <code>title</code> attribute to provide a tooltip and is inserted after the <code>input</code> element into the DOM. A call to <code>.button()</code> with some options together with a bit of class-mangling transforms the button into a Button widget that displays a down-arrow icon with rounded corners on the right (the input has rounded-corners on the left).</p>

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

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

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

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

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

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

<h4>.toggle()</h4>

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

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

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

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

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

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

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

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

<h4>Disabling stylesheets</h4>

<p>For fun, I thought, "What if instead of manipulating every DOM node and changing things, we just futz with the stylesheet?"  Could there be speed improvements there? I mean, the methods benchmarked above are plenty fast for everyday use, but what if I had 10,000 nodes on a page I wanted to show and hide? It would be slow just selecting them all. But, if I could manipulate the stylesheet, I could avoid the entire overhead. Let me just tell you that way is fraught with peril. </p>
<p>There are, of course, cross browser issues when manipulating stylesheets, since jQuery doesn't abstract them away for you. First, I tried to see if I could append a style tag with the css class as a string using jQuery, but got inconsistent results across browsers. Then I tried creating the stylesheet node and class using JavaScript, but there were different APIs and it ended up being too slow to justify. So finally, forgoing an attempt to do this in a programmatic way, I ended up just writing a style tag with a class in the head of the document. It's far too slow to create the stylesheet programmatically, but if it's already there then it is trivial to give it an ID and use its disabled attribute.</p>
<div class="igBar"><span id="lhtml-28"><a href="#" onclick="javascript:showPlainTxt('html-28'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-28">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;style</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special_hide&quot;</span><span style="color: #000000;">&gt;</span></span>.special_hide { display: none; }<span style="color: #009900;"><span style="color: #000000;">&lt;/style&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- &nbsp;... &nbsp;--&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special_hide&quot;</span><span style="color: #000000;">&gt;</span></span>Special hide DIV<span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />
<p>Then in javascript&hellip;</p>

<div class="igBar"><span id="ljavascript-29"><a href="#" onclick="javascript:showPlainTxt('javascript-29'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-29">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#special_hide'</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;">'disabled, '</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #3366CC;">');</span></div></li>
</ol></div>
</div></div><br />
<p>and BAM, you just displayed all of your elements with a class of &ldquo;special_hide&rdquo;. To hide them all again, just do&hellip;</p>

<div class="igBar"><span id="ljavascript-30"><a href="#" onclick="javascript:showPlainTxt('javascript-30'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-30">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#special_hide'</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;">'disabled'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'false'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>and they are now all hidden.  The total javascript processing time was 0-1ms across all browsers.  The only javascript you are doing is changing an attribute.  Of course, there is still the time the browser takes to reflow and repaint the page, but you've virtually eliminated all the javascript processing time. If you call any of the other methods, <code>.toggle()</code>, <code>.hide()</code>, or <code>.css()</code>, this method will stop working on those elements because they set the css style inline, which has higher precedence than other css. To make this method work again, simply do a <code>.css(‘display’, ‘’)</code> to remove the inline style.  This method also requires the most work on your part, because you have to define the class and give the class to all of the elements on the page you want to show/hide at the same time, but if you are dealing with extremely large sets, this might just be worth it.                                                                                                                                                                                                                               </p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>Call the collapsible plugin on the heading or other element that you're using to show related content; the plugin then assumes that the next element in the source order is the content block to show/hide. For example, on DOM ready you can call the plugin on all H2 elements:</p>

<div class="igBar"><span id="ljavascript-38"><a href="#" onclick="javascript:showPlainTxt('javascript-38'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-38">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">collapsible</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<h4>Where to download it</h4>
<p>If you've already purchased <em>Designing with Progressive Enhancement</em>, you can download all twelve widgets at the <a href="http://filamentgroup.com/dwpe#codeexamples">code examples download page</a>.</p>

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

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

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

<h4>Thoughts? Feedback?</h4>
<p>We'd love to hear what you think of this plugin. Please <a href="http://www.filamentgroup.com/lab/expand_and_collapse_content_accessibly_with_progressive_enhancement_jquery/#comments">leave us a comment on our blog</a>!</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/HcxRenunsns" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/04/accessible-showing-and-hiding/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2010/04/accessible-showing-and-hiding</feedburner:origLink></item>
		<item>
		<title>Flip your Tip: Keeping the Event-delegation Tooltip in View</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/ezT3s1NRTMg/flip-your-tip-keeping-the-event-delegation-tooltip-in-view</link>
		<comments>http://www.learningjquery.com/2010/04/flip-your-tip-keeping-the-event-delegation-tooltip-in-view#comments</comments>
		<pubDate>Thu, 15 Apr 2010 16:51:38 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Effects]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1047</guid>
		<description>Before we begin, please accept my apologies for not posting this tutorial sooner. I know at least two or three people were beginning to wonder if I'd ever finish what I started with this tooltip series. Please also forgive me if the phrase "flip your tip" has a double meaning in some ultra-hip corner of [...]</description>
			<content:encoded><![CDATA[<p>Before we begin, please accept my apologies for not posting this tutorial sooner. I know at least two or three people were beginning to wonder if I'd ever finish what I started with this tooltip series. Please also forgive me if the phrase "flip your tip" has a double meaning in some ultra-hip corner of the universe. If it does, I can assure you that I am unaware of it&mdash;ignorant and unhip, to be sure, but more important, innocent. Now, on with the show.</p>
<h4>Quick Review</h4>
<p>In my last three tutorials, I discussed how to put together a very simple tooltip, and I introduced a different feature or concept in each one. In an effort to continue in the spirit of simplicity, I will refrain from repeating the explanations of previous posts and instead simply direct your attention to them before we begin:</p>
<ul>
  <li><a href="http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements">Simple Tooltip for Huge Number of Elements</a> </li>
  <li><a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips">Binding Multiple Events to Reduce Redundancy with Event Delegation Tooltips</a></li>
<li><a href="http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips">Using setTimeout to Delay Showing Event Delegation Tooltips</a></li>

</ul>
<span id="more-1047"></span>
<h4>Setup</h4>
<p>Now I'd like to round off this series with a little exploration of one way to keep the tooltip within view when it might otherwise get clipped to the right or the bottom of the viewable area.</p>
<p>As with the previous tooltip tutorials, I start by setting a few variables:</p>
<div class="igBar"><span id="ljavascript-42"><a href="#" onclick="javascript:showPlainTxt('javascript-42'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-42">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</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=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</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="background-color: transparent;">&nbsp; &nbsp; $win <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip;</div></li>
</ol></div>
</div></div><br />
<p>The first line creates the tooltip container, hides it, appends it to the body, and stores a reference to it for later use. The next line is more for convenience than anything else (though it might provide nominal performance benefit), since I'll be calling jQuery methods on the <code>window</code> object a few times. The third line will be used as a reference to a <code>setTimeout</code> function for delaying the tooltip's visibility.</p>
<h4>Position the Tip</h4>
<p>Okay. So far, so boring. But here is where things start getting fun. I have a few more pieces I want to store and retrieve, but since they're all related, I figured it would be nice to have them be properties of a single <code>tip</code> object:</p>
<div class="igBar"><span id="ljavascript-43"><a href="#" onclick="javascript:showPlainTxt('javascript-43'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-43">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> tip <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; title<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; delay<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">300</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="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: #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="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> positions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dimensions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; x<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $win.<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="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $liveTip.<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></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; y<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $win.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> $win.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=height"><span style="">height</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="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $liveTip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=outerHeight"><span style="">outerHeight</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#93;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> axis <span style="color: #000066; font-weight: bold;">in</span> dimensions <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dimensions<span style="color: #009900;">&#91;</span>axis<span style="color: #009900;">&#93;</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;">&lt;</span>dimensions<span style="color: #009900;">&#91;</span>axis<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> positions<span style="color: #009900;">&#91;</span>axis<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; positions<span style="color: #009900;">&#91;</span>axis<span style="color: #009900;">&#93;</span> <span style="color: #339933;">-=</span> dimensions<span style="color: #009900;">&#91;</span>axis<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; positions<span style="color: #009900;">&#91;</span>axis<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $liveTip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> positions.<span style="color: #660066;">y</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; left<span style="color: #339933;">:</span> positions.<span style="color: #660066;">x</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Since the position needs to be calculated each time the tooltip is displayed, I've made <code>position</code> a function&mdash;in other words, a <em>method</em> of the <code>tip</code> object. This is the part that does the hard work of figuring out where exactly to put the tooltip and whether or not to "flip" it above or to the left of the mouse position. Because this method is going to be called repeatedly as the mouse moves over a link, I wanted to make it as bare-bones as possible. I defined two objects. The first one contains the <code>x</code> and <code>y</code> mouse coordinates. The other contains two sets of numbers: <strong>(x)</strong> the inner width of the browser window and outer width of the tooltip, and <strong>(y)</strong> the distance from the top of the document to the bottom edge of the viewable area and the outer height of the tooltip.
<p>Notice that the properties of the <code>dimensions</code> object have the same names as the properties of the <code>positions</code> object. I did that so I could easily loop through the properties of one and use the same loop variable for both, as shown in lines 18-26 above. Within that little loop, I'm testing to see if the tooltip, plus the mouse position, plus our user-defined "offset," is greater than the window dimension&mdash;on the <em>x</em> axis and then on the <em>y</em>. If it is, I subtract the tooltip's dimension and its offset from the mouse position; if not, I just add the offset to the mouse position.</p>
<p>Finally, I set the <code>top</code> and <code>left</code> style properties of the tooltip with the appropriate coordinates. I suppose it would have been cool to name the properties of my two objects "<code>left</code>" and "<code>top</code>". That way, I could have written <code>$liveTip.css(positions)</code>. But, <code>x</code> and <code>y</code> make more sense to me as I read them, so I figured it would be worth a few extra bytes to help me remember what I was doing here when I look back at the script later on.</p>
<h4>Wire it up</h4>
<p>Now that the tooltip positioning is taken care of, it's time to hook it up to some events. If you read <a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips">Binding Multiple Events to Reduce Redundancy with Event Delegation Tooltips</a>, you may remember that I used the <code>.bind()</code> method and passed in three events: <code>mouseover</code>, <code>mouseout</code>, and <code>mousemove</code>. That seemed to work fine, but now that jQuery 1.4.2 has provided us with the more convenient <a href="http://api.jquery.com/delegate"><code>.delegate()</code></a> method, we can use that one instead. Jordan Boesch recently wrote a nice introductory article on <a href="http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2">Using Delegate and Undelegate in jQuery</a>, so check that out if you're uncertain about what the method does. One thing I'd like to note about it, though, is that it's currently the only jQuery method (of its kind) that doesn't map the <code>this</code> keyword in the callback function to the current element within the matched set. In the code below, for example, the callback function for other methods would map <code>this</code> to the "#mytable" element; for <code>.delegate()</code>, the function maps to its first argument, "a" &mdash; when it is within "#mytable" and when it or one of its ancestors is the <code>event.target</code> element. I hope this little .delegate() excursion didn't confuse matters. Suffice it to say that the method allows us to use <code>this</code> for the link we want to act on, instead of going through the rigamarole of checking for the event target ourselves as I did in my previous tooltip articles. </p>

<div class="igBar"><span id="ljavascript-44"><a href="#" onclick="javascript:showPlainTxt('javascript-44'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-44">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'mouseover mouseout mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; tip.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> link.<span style="color: #660066;">title</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip <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: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; tip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=position"><span style="">position</span></a><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tip.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; .<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: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=fadeIn"><span style="">fadeIn</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">200</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> tip.<span style="color: #660066;">delay</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseout'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> tip.<span style="color: #660066;">title</span> <span style="color: #339933;">||</span> link.<span style="color: #660066;">title</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=removeData"><span style="">removeData</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; clearTimeout<span style="color: #009900;">&#40;</span>showTip<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mousemove'</span> &#038;& $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; tip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=position"><span style="">position</span></a><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Since a lot of what's going on in this code snippet has already been discussed in my previous posts, I'll just mention a couple things.</p>
<p>The tip positioning occurs in line 13, triggered by the <code>mouseover</code> event, and in line 34, triggered by <code>mousemove</code>. It only gets triggered in the <code>mousemove</code>, however, if the mouse has been over the link for the time specified in <code>tip.delay</code> for the <code>setTimeout</code>. That's where the link's "tipActive" data attribute is set. </p>
<p>This time around, I also added a little fade effect to show the tooltip. As a safeguard, I hide the tooltip and reset its opacity to 0 (using <code>.fadeout()</code> with a 0ms duration) before fading it in, because jQuery will only fade in elements if they are hidden to begin with. This extra step is only  necessary if the fade-in duration is greater than <code>tip.delay</code>.</p>
<h4>Conclusion</h4>
<p>So that wraps up this tooltip series. As always, if you see something that I could do better, please leave a comment. I always appreciate learning better ways of plying the craft. And if you have any questions about what's going on in the code, please point out what you'd like me to clarify. </p>
<p>To see this one in action, visit the <a href="http://test.learningjquery.com/tooltips/fliptip.html">Flip Tip demo page</a>. You can also look at the <a href="http://test.learningjquery.com/tooltips/fliptip.js">complete JavaScript file</a> or download a <a href="http://test.learningjquery.com/tooltips/fliptip.zip">zip of both the HTML and JavaScript</a>.</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/ezT3s1NRTMg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/04/flip-your-tip-keeping-the-event-delegation-tooltip-in-view/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2010/04/flip-your-tip-keeping-the-event-delegation-tooltip-in-view</feedburner:origLink></item>
		<item>
		<title>Spring 2010 jQuery Conference: San Francisco Bay Area</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/dH7TmSMK-pU/spring-2010-jquery-conference-san-francisco-bay-area</link>
		<comments>http://www.learningjquery.com/2010/03/spring-2010-jquery-conference-san-francisco-bay-area#comments</comments>
		<pubDate>Mon, 29 Mar 2010 13:26:17 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Bay Area]]></category>
		<category><![CDATA[California]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[San Francisco]]></category>
		<category><![CDATA[West Coast]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1057</guid>
		<description>Since the fall of 2007, jQuery developers around the world have been making an annual pilgrimage to Boston, Massachusetts, to meet jQuery team members, JavaScript luminaries, and other jQuery developers at the official jQuery Conference. This spring, in response to growing demand, the jQuery Project is offering its first-ever official jQuery conference on the U.S [...]</description>
			<content:encoded><![CDATA[<p>Since the fall of 2007, jQuery developers around the world have been making an annual pilgrimage to Boston, Massachusetts, to meet jQuery team members, JavaScript luminaries, and other jQuery developers at the official jQuery Conference. This spring, in response to growing demand, the jQuery Project is offering its first-ever <a href="http://events.jquery.org/2010/sf-bay-area/">official jQuery conference on the U.S west coast</a>. The conference will take place April 24&ndash;25 in Mountain View, California, and will include <a href="http://events.jquery.org/2010/sf-bay-area/speakers/">top-notch presentations</a> from the jQuery team, as well as other web development and performance experts. Additionally, the conference will be preceded by a one-day intensive jQuery training course in downtown San Francisco, led by <a href="http://appendto.com/">appendTo</a> a leading jQuery training and consulting company.</p>
<span id="more-1057"></span>
<p>Priced at $199, the two-day conference is one of the best bargains you'll find anywhere for a full-on tech conference. The jQuery team is committed to keeping the price as low as possible, relying on the <a href="http://jquery.org/sponsors">support</a> of <a href="http://events.jquery.org/2010/sf-bay-area/sponsors/">sponsors</a> to help defray the cost of registration. jQuery developers from beginner to advanced will learn a ton, have a great time, and meet many others who are using jQuery in innovative ways to make the web a better place.</p>
<p>On a personal note, this will be the first jQuery conference ever that I won't be able to attend (regrettably, a scheduling conflict is keeping me home this time). But having heard many of the speakers in the past and having been involved a bit in the planning of this conference, I can assure you that <em>you</em> won't want to miss it. While we hope to continue offering a west-coast event on an annual basis, as we have done with the east-coast event a lot will depend on the level of interest and attendance this time around. So, what are you waiting for? <a href="http://events.jquery.org/2010/sf-bay-area/register/">Sign up now</a>!</p>
<p>One brief personal plug: I will be teaching a <a href="http://ideafoundry.info/jquery">3-day jQuery class</a> June 2&ndash;4 in beautiful Holland, Michigan. If for some reason you can't make it to the conference in April&mdash;or even if you can&mdash;you might want to consider taking the class. This will be the fourth class I've taught with <a href="http://ideafoundry.info/">Idea Foundry</a>, and if it's anything like the others, we'll have a blast, with lots and lots of great learning.</p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/dH7TmSMK-pU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/03/spring-2010-jquery-conference-san-francisco-bay-area/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2010/03/spring-2010-jquery-conference-san-francisco-bay-area</feedburner:origLink></item>
		<item>
		<title>Detecting Ajax Events on the Server</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/rkoOBxKRfhE/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>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-52"><a href="#" onclick="javascript:showPlainTxt('javascript-52'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-52">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #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="background-color: transparent;">&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="background-color: transparent;"><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-53"><a href="#" onclick="javascript:showPlainTxt('php-53'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">PHP:</span><br /><div id="php-53">
<div class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><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="background-color: transparent;">&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="background-color: transparent;"><span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color:#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="background-color: transparent;">&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="background-color: transparent;"><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="background-color: transparent;">&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="background-color: transparent;"><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-54"><a href="#" onclick="javascript:showPlainTxt('ruby-54'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">RUBY:</span><br /><div id="ruby-54">
<div class="ruby" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><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="background-color: transparent;">&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="background-color: transparent;"><span style="color:#9966CC; font-weight:bold;">else</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;"><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-55"><a href="#" onclick="javascript:showPlainTxt('html-55'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-55">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;"><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-56"><a href="#" onclick="javascript:showPlainTxt('php-56'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">PHP:</span><br /><div id="php-56">
<div class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;"><span style="color:#000000; font-weight:bold;">?&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;!DOCTYPE html&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;html&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &lt;head&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;&lt;title&gt;Email Signup&lt;/title&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .success { color: green }</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .error &nbsp; { color: red }</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;&lt;/style&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &lt;/head&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &lt;body&gt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &lt;/body&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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-57"><a href="#" onclick="javascript:showPlainTxt('javascript-57'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-57">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&lt;script</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> 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="background-color: transparent;">&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="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> 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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #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="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="">&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-58"><a href="#" onclick="javascript:showPlainTxt('php-58'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">PHP:</span><br /><div id="php-58">
<div class="php" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color:#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="background-color: transparent;">&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="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color:#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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&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>
<img src="http://feeds.feedburner.com/~r/LearningJquery/~4/rkoOBxKRfhE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/03/detecting-ajax-events-on-the-server/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2010/03/detecting-ajax-events-on-the-server</feedburner:origLink></item>
		<item>
		<title>Using Delegate and Undelegate in jQuery 1.4.2</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/aXoZH0PEu8U/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>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-71"><a href="#" onclick="javascript:showPlainTxt('javascript-71'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-71">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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="background-color: transparent;">&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="background-color: transparent;"><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-72"><a href="#" onclick="javascript:showPlainTxt('javascript-72'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-72">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// append an image </span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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-73"><a href="#" onclick="javascript:showPlainTxt('javascript-73'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-73">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// FAILS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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="background-color: transparent;"><span style="color: #006600; font-style: italic;">// FAILS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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-74"><a href="#" onclick="javascript:showPlainTxt('javascript-74'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-74">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// FAILS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document.<span style="color: #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-75"><a href="#" onclick="javascript:showPlainTxt('javascript-75'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-75">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// WORKS</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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-76"><a href="#" onclick="javascript:showPlainTxt('javascript-76'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-76">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Using .live()</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Using .die()</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&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="background-color: transparent;">&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="background-color: transparent;"><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-77"><a href="#" onclick="javascript:showPlainTxt('javascript-77'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-77">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Using .delegate()</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&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="background-color: transparent;">&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="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// Using .undelegate()</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&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-78"><a href="#" onclick="javascript:showPlainTxt('javascript-78'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-78">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// .bind() way</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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="background-color: transparent;">&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="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// .delegate() way</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span>.<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="background-color: transparent;">&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="background-color: transparent;"><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-79"><a href="#" onclick="javascript:showPlainTxt('javascript-79'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-79">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// This works wonderfully</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br /> 
<p>An error will be thrown when you try to do:</p>
<div class="igBar"><span id="ljavascript-80"><a href="#" onclick="javascript:showPlainTxt('javascript-80'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-80">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// FAILS!</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>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-81"><a href="#" onclick="javascript:showPlainTxt('javascript-81'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-81">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> 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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<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="background-color: transparent;">&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="background-color: transparent;">&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="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #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-82"><a href="#" onclick="javascript:showPlainTxt('javascript-82'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-82">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #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="background-color: transparent;">&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="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'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="background-color: transparent;">&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="background-color: transparent;"><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>
<img src="http://feeds.feedburner.com/~r/LearningJquery/~4/aXoZH0PEu8U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2010/03/using-delegate-and-undelegate-in-jquery-1-4-2</feedburner:origLink></item>
		<item>
		<title>Using setTimeout to Delay Showing Event-Delegation Tooltips</title>
		<link>http://feedproxy.google.com/~r/LearningJquery/~3/6hA6Wopy05s/using-settimeout-to-delay-showing-event-delegation-tooltips</link>
		<comments>http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips#comments</comments>
		<pubDate>Wed, 16 Dec 2009 17:47:22 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[setTimeout]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=972</guid>
		<description>In my last two tutorials, I explained how to use event delegation for showing and hiding tooltips. In a comment on the first one, Jan Aagaard asked how we might go about enhancing the script by adding a small delay before showing a tooltip. The answer lies with two JavaScript functions, setTimeout() and clearTimeout(). 

Setting [...]</description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements" title="
Simple Tooltip for Huge Number of Elements">last</a> <a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips" title="
Binding Multiple Events to Reduce Redundancy with Event-Delegation">two</a> tutorials, I explained how to use event delegation for showing and hiding tooltips. In a comment on the first one, <a href="http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements/#div-comment-79280">Jan Aagaard asked</a> how we might go about enhancing the script by adding a small delay before showing a tooltip. The answer lies with two JavaScript functions, <code>setTimeout()</code> and <code>clearTimeout()</code>. </p>
<span id="more-972"></span>
<h4>Setting Up</h4>
<p>Picking up where we left off <a href="http://www.learningjquery.com/2009/12/binding-multiple-events-to-reduce-redundancy-with-event-delegation-tooltips" title="
Binding Multiple Events to Reduce Redundancy with Event-Delegation">last time</a>, I'm going to create the tooltip div, declare a couple variables, and then bind "mouseover," "mousemove," and "mouseout" all at once. In addition to <code>$livetip</code> and <code>tipTitle</code>, I'm declaring <code>showTip</code> (and leaving it undefined for the moment) and <code>delay</code>. The <code>showTip</code> variable will be used as a reference for the <code>setTimeout</code> function, and <code>delay</code> will be used to set the time after which the function within <code>setTimeout</code>'s argument will execute (in this case, 300ms).</p>
<div class="igBar"><span id="ljavascript-86"><a href="#" onclick="javascript:showPlainTxt('javascript-86'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-86">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</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=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip<span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; delay <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="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 mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// ... code continues</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>Of course, you don't <em>have to</em> use a variable for the delay, but I like to have that sort of setting up top where I can find it easily. Also, if I ever decide to convert this script into a plugin, having the delay stored in a variable will make it simpler to convert it to an option.</p>
<h4>Setting the Timeout</h4>
<p>Inside the <code>.bind()</code>, and after the business of determining whether the event is being triggered by a link or one of its descendants, the <code>setTimeout</code> is called on line 12:</p>
<div class="igBar"><span id="ljavascript-87"><a href="#" onclick="javascript:showPlainTxt('javascript-87'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-87">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</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=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip<span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; delay <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="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 mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>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;">'a'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$link.<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> <span style="color: #000066; font-weight: bold;">return</span>; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip <span style="color: #339933;">=</span> window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; tipTitle <span style="color: #339933;">=</span> link.<span style="color: #660066;">title</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="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;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=show"><span style="">show</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> delay<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// ... code continues</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p><code>setTimeout</code> is actually a method of the <code>window</code> object; as such, the <code>this</code> keyword inside its function argument will refer to <code>window</code>. It's not of much concern here because, with event delegation, we're relying on <code>event.target</code> rather than <code>this</code>, but I thought it might be worth mentioning anyway. </p>
<p>Note that within the <code>setTimeout</code> function, I'm storing a little data (line 13) for the link to indicate that the tooltip is active on it. This <code>tipActive</code> information then determines what to do on mouseout.</p>
<h4>Clearing the Timeout</h4>
<p>On mouseout, if the link has the <code>tipActive</code> data, that data is removed (line 29, below), the tooltip is hidden (line 30), and the link's original title attribute value is restored if tipTitle has a value (line 31). If that <code>tipActive</code> data is <em>not</em> associated with the link, then the timeout that was set on mouseover is reset with <code>clearTimeout</code> (line 33).</p>
<div class="igBar"><span id="ljavascript-88"><a href="#" onclick="javascript:showPlainTxt('javascript-88'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-88">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> $liveTip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;livetip&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</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=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> tipTitle <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip<span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; delay <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">300</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mytable'</span><span style="color: #009900;">&#41;</span>.<a href="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 mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> $link <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>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;">'a'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>$link.<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> <span style="color: #000066; font-weight: bold;">return</span>; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> $link<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseover'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; showTip <span style="color: #339933;">=</span> window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; tipTitle <span style="color: #339933;">=</span> link.<span style="color: #660066;">title</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="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;">'&lt;div&gt;'</span> <span style="color: #339933;">+</span> tipTitle <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;&lt;div&gt;'</span> <span style="color: #339933;">+</span> link.<span style="color: #660066;">href</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=show"><span style="">show</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> delay<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mouseout'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=removeData"><span style="">removeData</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $liveTip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; link.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> tipTitle <span style="color: #339933;">||</span> link.<span style="color: #660066;">title</span>; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; window.<span style="color: #660066;">clearTimeout</span><span style="color: #009900;">&#40;</span>showTip<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mousemove'</span> &#038;& $link.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tipActive'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $liveTip.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; left<span style="color: #339933;">:</span> event.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;color:#800000;">12</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The mousemove part hasn't changed from what it was in the previous post, except that it first checks if the "tipActive" data is assigned to the link. I figured that setting the <code>top</code> and <code>left</code> properties of the tooltip is probably more work than doing a little check on the link's data, so why bother setting them if I don't have to?</p>
<p>So, that's it. Just add a simple <code>setTimeout</code> delay and conditionally clear it. If you want to set a delay on mouseout, you can do that the same way.</p>
<p><a href="http://test.learningjquery.com/tooltips/delaytip.html">Check out the demo</a> or <a href="http://test.learningjquery.com/tooltips/delaytip.zip">download the zip</a> (or neither, if you're stubborn).</p>

<h4>Flipping the Tip</h4>
<p>Next time, I'll show how to flip the tooltip from the right side of the cursor to the left if it would otherwise get cut off by the edge of the browser window. </p><img src="http://feeds.feedburner.com/~r/LearningJquery/~4/6hA6Wopy05s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<feedburner:origLink>http://www.learningjquery.com/2009/12/using-settimeout-to-delay-showing-event-delegation-tooltips</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced)
Database Caching 33/36 queries in 0.010 seconds using disk

Served from: www.learningjquery.com @ 2010-08-26 16:56:27 -->
