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

<channel>
	<title>Eric Hynds &#187; jQuery</title>
	<atom:link href="http://www.erichynds.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.erichynds.com</link>
	<description>Web Developer&#039;s Ramblings on JavaScript, jQuery, ColdFusion, MySQL, and other technologies.</description>
	<lastBuildDate>Mon, 02 Jan 2012 22:55:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using Deferreds in jQuery 1.5</title>
		<link>http://www.erichynds.com/jquery/using-deferreds-in-jquery/</link>
		<comments>http://www.erichynds.com/jquery/using-deferreds-in-jquery/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 18:35:46 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[deferreds]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=1058</guid>
		<description><![CDATA[Deferreds, new in jQuery 1.5, decouple logic dependent on the outcome of a task from the task itself. They’re nothing new to the JavaScript scene; Mochikit and Dojo have implemented them for some time, but with Julian Aubourg&#8217;s AJAX rewrite landing in 1.5, deferreds in jQuery was the logical next step. With deferreds, multiple callbacks [...]


Related posts:<ol><li><a href='http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/' rel='bookmark' title='A Recursive setTimeout Pattern'>A Recursive setTimeout Pattern</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fusing-deferreds-in-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fusing-deferreds-in-jquery%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Deferreds, new in jQuery 1.5, decouple logic dependent on the outcome of a task from the task itself.  They’re nothing new to the JavaScript scene; Mochikit and Dojo have implemented them for some time, but with <a href="http://jaubourg.net/">Julian Aubourg&#8217;s</a> AJAX rewrite landing in 1.5, deferreds in jQuery was the logical next step.  With deferreds, multiple callbacks can be bound to a task&#8217;s outcome, and any of these callbacks can be bound even after the task is complete.  The task in question may be asynchronous, but not necessarily. </p>
<p>What&#8217;s more, deferreds are now built-into <code>$.ajax()</code> so you&#8217;ll get them automatically.  Handlers can now be bound like this:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// $.get, an ajax request, is asynchronous by default.</span>
<span style="color: #003366; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo.htm'</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> response <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// do something with the response</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">error</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>
      <span style="color: #006600; font-style: italic;">// do something if the request failed</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// this might execute before the $.get() above is complete</span>
doSomethingAwesome<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// something additional to execute upon success, which might, or might not,</span>
<span style="color: #006600; font-style: italic;">// have fired by now.  With $.ajax deferreds built-in, it doesn't matter.</span>
req.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> response <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// do something more with the response</span>
   <span style="color: #006600; font-style: italic;">// this will fire when success normally fires, or fire immediately</span>
   <span style="color: #006600; font-style: italic;">// if prior success callbacks have already fired</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<p>We are no longer limited to one success, error, or complete handler anymore, and instead of simple callback functions, these hooks are now self-managed first-in, first-out callback queues.</p>
<p>As shown in the example above, callbacks may be attached even after the AJAX request &ndash; or any observable task &ndash; has completed.  For code organization this is great; the days of long unwieldy callbacks may be over.  It’s almost as if <code>$.queue()</code> meets pub/sub.<span id="more-1058"></span></p>
<p>Digging a little deeper here, imagine a scenario where we want to call a function after several concurrent AJAX requests have completed.  This is easily accomplished with <code>$.when()</code>, deferred&#8217;s little helper method:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> doAjax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo.htm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> doMoreAjax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bar.htm'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span> doAjax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> doMoreAjax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">then</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>
      console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'I fire once BOTH ajax requests have completed!'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">fail</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>
      console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'I fire if one or more requests failed.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<p class="center"><a href="http://jsfiddle.net/ehynds/Mrqf8/">View this example on jsFiddle</a></p>
<p>The reason this works is because all of jQuery&#8217;s AJAX methods now return an object containing a &quot;promise&quot;, which is used to track the asynchronous request. <strong>The promise is a read-only view into the result of the task.</strong> Deferreds look for the presence of a <code>promise()</code> method to determine whether an object is observable or not.  The <code>$.when()</code> waits for all its AJAX requests to execute, and once they do, the callbacks attached to the <code>$.when()</code> via  <code>.then()</code> and <code>.fail()</code> will fire as appropriate (depending on task&#8217;s success or failure state). The callbacks fire in the order they were assigned.</p>
<p>It gets better: all deferred&#8217;s methods accept either functions or arrays of functions, so you can build your behaviors and assign them all with one call, or in separate calls, as you please.</p>
<p><code>$.ajax()</code> returns an object packed with other deferred-related methods.  I discussed <code>promise()</code>, but you’ll also find <code>then()</code>, <code>success()</code>, <code>error()</code>, and a host of others.  You don’t have access to the complete deferred object, though; only the promise, callback-binding methods, and the <code>isRejected()</code> and <code>isResolved()</code> methods, which can be used to check the state of the deferred.</p>
<p>But why not return the whole object? If this were the case, it would be possible to muck with the works, maybe pragmatically &quot;resolve&quot; the deferred, causing all bound callbacks to fire before the AJAX request had a chance to complete.  <strong>Therefore, to avoid potentially breaking the whole paradigm, only return the <code>dfd.promise()</code></strong>.</p>
<h2>Registering Callbacks</h2>
<p>In the examples thus far I&#8217;ve used the <code>then()</code>, <code>success()</code>, and <code>fail()</code> methods to register callbacks onto the deferred, but there are more methods available to you, especially when working with AJAX deferreds.  The method you choose ultimately depends on the resolution state(s) you’d like to bind to.</p>
<p>Available to all deferreds (AJAX, <code>$.when</code>, and those created manually):</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">.<span style="color: #660066;">then</span><span style="color: #009900;">&#40;</span> doneCallbacks<span style="color: #339933;">,</span> failedCallbacks <span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span> doneCallbacks <span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">fail</span><span style="color: #009900;">&#40;</span> failCallbacks <span style="color: #009900;">&#41;</span></pre></div></div>

<p></code></p>
<p>AJAX deferreds have three additional methods, two of which map to one of the above.  They are provided as semantic alternatives and match the names of the &quot;old&quot; handlers we&#8217;re all used to:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// &quot;success&quot; and &quot;error&quot; map to &quot;done&quot; and &quot;fail&quot; respectively.</span>
.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span> doneCallbacks <span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span> failCallbacks <span style="color: #009900;">&#41;</span></pre></div></div>

<p></code></p>
<p>You can register a <code>complete</code> handler that&#8217;ll fire regardless of the success or failure state of the request. Unlike <code>success</code> and <code>error</code>, <code>complete</code> is actually an alias to the <code>done</code> method of a separate deferred.  This separate deferred, created internally by <code>$.ajax()</code>, is resolved after an AJAX request completes, regardless of the outcome.</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">.<span style="color: #660066;">complete</span><span style="color: #009900;">&#40;</span> completeCallbacks <span style="color: #009900;">&#41;</span></pre></div></div>

<p></code></p>
<p>Therefore, the following three examples are equivalent (success reads better than done in the context of an AJAX request, don&#8217;t you think?)</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/foo/&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span> fn <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// same as:</span>
&nbsp;
$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/foo/&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span> fn <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// same as:</span>
&nbsp;
$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/foo/&quot;</span><span style="color: #339933;">,</span> fn <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<h2>Creating your own Deferred</h2>
<p>We know that <code>$.ajax</code> and <code>$.when</code> implement the deferred API internally, but you can also create your own implementations:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/foo/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> showDiv<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> dfd <span style="color: #339933;">=</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> dfd.<span style="color: #660066;">resolve</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> dfd.<span style="color: #660066;">promise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> showDiv<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">then</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> ajaxResult <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'The animation AND the AJAX request are both done!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// 'ajaxResult' is the server's response</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<p class="center"><a href="http://jsfiddle.net/ehynds/JSw5y/">View this example on jsFiddle</a></p>
<p>Inside <code>showDiv()</code> I&#8217;m creating a new deferred object, performing an animation, and returning the promise.  The deferred is resolved (think of <code>dequeue()</code> if you’re familiar with jQuery&#8217;s queuing methods) after the <code>fadeIn()</code> call completes.  Between the time the promise is returned and the deferred is resolved, a <code>then()</code> callback is registered to the successful completion of both asynchronous tasks.  Therefore, once both tasks resolve, the callback is fired.</p>
<p><code>getData()</code> returns an object with a promise method, which allows <code>$.when()</code> to observe its eventual resolution.  The manually steps we took to return a promise in <code>showDiv()</code> is handled for us internally by <code>$.ajax()</code> and <code>$.when()</code>.</p>
<div class="update">
 <strong>1/15/2011:</strong> As Julian pointed out in the comments, the above syntax can be shortened using the <code>$.Deferred(fn).promise()</code> signature.  The following two approaches to creating a deferred are equivalent:
</div>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> showDiv<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> dfd <span style="color: #339933;">=</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> dfd.<span style="color: #660066;">resolve</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> dfd.<span style="color: #660066;">promise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// same as:</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> showDiv<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> dfd <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> dfd.<span style="color: #660066;">resolve</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">promise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Defer your Deferreds</h2>
<p>We could take this one step further by registering individual callbacks to both <code>getData()</code> and <code>showDiv()</code>, as well as registering their individual promises onto one &quot;master&quot; deferred.  </p>
<p>If you wanted something to happen on the success of <code>getData()</code> and on the success of <code>showDiv()</code> (independently of the other), as well as on the success of both <code>getData()</code> and <code>showDiv()</code> combined, simply register a callback to their individual deferreds, and tie them together with <code>$.when</code>:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/foo/'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">success</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>
      console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Fires after the AJAX request succeeds'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> showDiv<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> dfd <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> dfd.<span style="color: #660066;">resolve</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">promise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> showDiv<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">then</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> ajaxResult <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Fires after BOTH showDiv() AND the AJAX request succeed!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// 'ajaxResult' is the server’s response</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<p class="center"><a href="http://jsfiddle.net/ehynds/W3cQc/">View this example on jsFiddle</a></p>
<h2>Chaining Hotness</h2>
<p>Deferred callbacks can be chained so as long as a promise is returned from the function. Here&#8217;s a real world example (via <a href="http://www.twitter.com/#!/ajpiano">@ajpiano</a>!)</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> saveContact<span style="color: #009900;">&#40;</span> row <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> $.<span style="color: #660066;">tmpl</span><span style="color: #009900;">&#40;</span>templates<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;contact-form&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      valid <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      messages <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
      dfd <span style="color: #339933;">=</span> $.<span style="color: #660066;">Deferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">/*
      bunch of client-side validation here
   */</span>
&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>
      dfd.<span style="color: #660066;">resolve</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
         success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
         errors<span style="color: #339933;">:</span> messages
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      form.<span style="color: #660066;">ajaxSubmit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
         dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;json&quot;</span><span style="color: #339933;">,</span>
         success<span style="color: #339933;">:</span> dfd.<span style="color: #660066;">resolve</span><span style="color: #339933;">,</span>
         error<span style="color: #339933;">:</span> dfd.<span style="color: #660066;">reject</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000066; font-weight: bold;">return</span> dfd.<span style="color: #660066;">promise</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
saveContact<span style="color: #009900;">&#40;</span> row <span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">then</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> response.<span style="color: #660066;">success</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #006600; font-style: italic;">// saving worked; rejoice</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #006600; font-style: italic;">// client-side validation failed</span>
         <span style="color: #006600; font-style: italic;">// output the contents of response.errors</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">fail</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// AJAX request failed</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<p>The <code>saveContact()</code> function first validates the form and saves the result into the variable valid. If validation fails, the deferred is resolved with an object containing a <code>success</code> boolean and an array of error messages. If the form passes validation, the deferred is resolved, except this time the success handler receives the response from the AJAX request. The <code>fail()</code> handler responds to 404, 500, and other HTTP errors that could prevent the AJAX request from succeeding.</p>
<h2>Non-observable Tasks</h2>
<p>Deferreds are particularly useful when the logic to execute may or may not be asynchronous, and you want to abstract that condition out of the mainline code. Your task might return a promise, but it might also return a string, object, or some other type. </p>
<p>In this example, the first time a &quot;launch application&quot; link is clicked on, an AJAX request tells the server to record (and return) the current timestamp.  The timestamp is stored in the element&#8217;s data cache after the AJAX request is complete.  The application only cares about the initial first click though, so on subsequent clicks, the timestamp is read out of the data cache instead of making an additional trip to the server.</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> startTask<span style="color: #009900;">&#40;</span> element <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> timestamp <span style="color: #339933;">=</span> $.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span> element<span style="color: #339933;">,</span> <span style="color: #3366CC;">'timestamp'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> timestamp <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">return</span> timestamp<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">return</span> $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/start-task/'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">success</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> timestamp <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           $.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span> element<span style="color: #339933;">,</span> <span style="color: #3366CC;">'timestamp'</span><span style="color: #339933;">,</span> timestamp <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#launchApplication'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</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> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   $.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span> startTask<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> timestamp <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#status'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&lt;p&gt;You first started this task on: '</span> <span style="color: #339933;">+</span> timestamp <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   loadApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<p>When <code>$.when()</code> recognizes that its first argument doesn&#8217;t have a promise (and therefore is not observable), it creates a new deferred object, resolves it with the data, and returns the promise from the deferred.  As such, something arbitrary without an initial promise can be observed.</p>
<p>One small gotcha to be aware of (which will most likely be addressed in the next maintenance release), is that you cannot defer an object that implements it’s own promise method. Deferreds are detected by the presence of a promise method, but jQuery doesn&#8217;t check to see if the promise actually returns a usable object.  Therefore, this will throw a syntax error:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
   promise<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: #006600; font-style: italic;">// do something</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
$.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">then</span><span style="color: #009900;">&#40;</span> fn <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></code></p>
<h2>Conclusion</h2>
<p>Deferreds introduce a new, robust approach to writing asynchronous tasks. Instead of focusing on how to organize callback logic into a singular callback, you can assign several individual actions to callback queues knowing that these will be executed, in context, without worrying so much about synchronicity. The info here might be a lot to digest, but once you get the hang of it, I think you&#8217;ll find writing asynchronous code to be much easier with deferreds.  </p>
<p style="color:#777; font-style:italic;">Many thanks to <a style="color:#676767" href="http://stevenblack.com/">Steven Black</a> for editing this post, as well as the #jquery-dev crew for reinforcing my understanding of deferreds.  <a href="http://twitter.com/#!/ajpiano" style="color:#676767">Adam</a> hooked me up with a sweet example, too.</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/' rel='bookmark' title='A Recursive setTimeout Pattern'>A Recursive setTimeout Pattern</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/using-deferreds-in-jquery/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>Using $.widget.bridge Outside of the Widget Factory</title>
		<link>http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/</link>
		<comments>http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 14:18:37 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[jquery ui]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=980</guid>
		<description><![CDATA[Within jQuery UI&#8217;s widget factory exists a little method called $.widget.bridge, which acts as a middle man between the object created with $.widget and the jQuery API. Because bridge is a public function you do not need jQuery UI or any other part of the widget factory to use it. This is awesome and i&#8217;mma [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/' rel='bookmark' title='Tips for Developing jQuery UI 1.8 Widgets'>Tips for Developing jQuery UI 1.8 Widgets</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/' rel='bookmark' title='jQuery UI MultiSelect Widget'>jQuery UI MultiSelect Widget</a></li>
<li><a href='http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/' rel='bookmark' title='A jQuery UI Growl/Ubuntu-like Notification Widget'>A jQuery UI Growl/Ubuntu-like Notification Widget</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fusing-jquery-ui-widget-factory-bridge%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fusing-jquery-ui-widget-factory-bridge%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Within jQuery UI&#8217;s widget factory exists a little method called <code>$.widget.bridge</code>, which acts as a middle man between the object created with <code>$.widget</code> and the jQuery API.  Because bridge is a public function you do not need jQuery UI or any other part of the widget factory to use it.  This is awesome and i&#8217;mma show you how.</p>
<p>What the widget factory calls &quot;bridge&quot; is an important pattern that affords us modularity and loose couping. Both <a href="http://alexsexton.com/?p=51">Alex Sexton</a> and <a href="http://jupiterjs.com/news/writing-the-perfect-jquery-plugin">Justin Meyer</a> do an excellent job explaining it, and I highly recommend you become familiar with this design pattern if you&#8217;re not already.</p>
<p><code>$.widget.bridge</code> does a few things:</p>
<ul>
<li>Connects a regular JavaScript object to the jQuery API.</li>
<li>Automatically creates instances of said object and stores it within the element(s) <code>$.data</code> cache.</li>
<li>Allows option changes after initialization.</li>
<li>Allows calls to public methods.</li>
<li>Prevents calls to private methods.</li>
<li>Prevents method calls on uninitialized objects.</li>
<li>Protects against multiple initializations.</li>
</ul>
<p>jQuery UI widgets are created using <code class="js">$.widget("foo.bar", {});</code> syntax to define an object from which instances will be created.  Given a DOM structure with five <code>.foo</code>&#8216;s, <code>$('.foo').bar();</code> will create five instances of your &quot;bar&quot; object.  <code>$.widget.bridge</code> works inside the factory by taking your base &quot;foo&quot; object and giving it a public API, so that you can create instances by writing <code>$('.foo').bar()</code>, and call methods by writing <code>$('.foo').bar('baz')</code>.<span id="more-980"></span></p>
<p>However, the object passed into <code>$.widget.bridge</code> can be your own, not one that was created with <code>$.widget()</code>.  But first, bridge has to make a few assumptions about your code for all this to work correctly:</p>
<ol>
<li>
		Your object must have both an <code>option</code> and an <code>_init</code> method.  Initialiation logic goes in <code>_init</code>, and logic to change an option after initilization goes in <code>option</code>.
	</li>
<li>
		Your <code>option</code> method returns the current instance.  If you attempt to re-initialize an instance on an element, <code>option</code> is called first, and immediately chained to that is <code>_init</code>.
	</li>
<li>
		Your constructor accepts two arguments: <code>options</code>, an object of configuration options, and <code>element</code>, the DOM element this instance was created on.
	</li>
</ol>
<p>That said, let&#8217;s create an object:</p>
<p><script src="http://gist.github.com/591632.js?file=widget.js"></script></p>
<p>Now hook it up to the bridge:</p>
<p><script src="http://gist.github.com/623931.js?file=bridge.js"></script></p>
<p>All this functionality and <code>$.widget.bridge</code> is less than 50 lines of code.  Here&#8217;s a <a href="http://github.com/jquery/jquery-ui/blob/1.8.5/ui/jquery.ui.widget.js#L81">link to the source</a> so you can study how it works.</p>
<p>Also, view source on <a href="http://sandbox.nemikor.com/extensible-jquery/">this page</a> by jQuery UI core member Scott Gonzales.  His code on that page walks through the <code>$.widget.bridge</code> concept and illustrates how to use it.</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/' rel='bookmark' title='Tips for Developing jQuery UI 1.8 Widgets'>Tips for Developing jQuery UI 1.8 Widgets</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/' rel='bookmark' title='jQuery UI MultiSelect Widget'>jQuery UI MultiSelect Widget</a></li>
<li><a href='http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/' rel='bookmark' title='A jQuery UI Growl/Ubuntu-like Notification Widget'>A jQuery UI Growl/Ubuntu-like Notification Widget</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Working With XML, jQuery, and JavaScript</title>
		<link>http://www.erichynds.com/jquery/working-with-xml-jquery-and-javascript/</link>
		<comments>http://www.erichynds.com/jquery/working-with-xml-jquery-and-javascript/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 16:44:34 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=886</guid>
		<description><![CDATA[A recent requirement of mine was to convert large CSV files to XML, and then build an accompanying interface to update &#38; manage the data.  jQuery was most useful for traversing &#38; manipulating the large DOM&#8217;s, but I did run across a few gotcha&#8217;s worthy of discussion.  There doesn&#8217;t seem to be much info on [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fworking-with-xml-jquery-and-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fworking-with-xml-jquery-and-javascript%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>A recent requirement of mine was to convert large CSV files to XML, and then build an accompanying interface to update &amp; manage the data.  jQuery was most useful for traversing &amp; manipulating the large DOM&#8217;s, but I did run across a few gotcha&#8217;s worthy of discussion.  There doesn&#8217;t seem to be much info on the interwebs about XML and jQuery outside of an XHR request, so here&#8217;s what I picked up along the way.</p>
<h2>You cannot pass an XML string to jQuery&#8217;s constructor</h2>
<p>Well <em>technically </em>you can, but it&#8217;s <a href="http://api.jquery.com/jQuery/#html" target="_blank">not</a> <a href="http://dev.jquery.com/ticket/6796#comment:2" target="_blank">supported</a> and jQuery will not treat the string as XML.  You&#8217;ll still be working with an HTML document object.  jQuery has no idea/doesn&#8217;t care that the elements are not valid HTML elements though, so this might be just fine as long as your XML remains detached from the DOM.  To feed XML to jQuery correctly, you must pass a parsed XML string (a la XML object) to jQuery&#8217;s constructor.</p>
<p>A <a href="http://dev.jquery.com/ticket/6693" target="_blank">new feature ticket</a> exists to add cross-browser XML compatibility to jQuery, whose summary yields another good nugget of info:</p>
<blockquote><p><em>As documented, the $(&#8220;&lt;html string&gt;&#8221;) method only supports parsing HTML strings, but users often try to parse XML strings with it. This doesn&#8217;t work since jQuery uses the .innerHTML property for parsing and implicitly enforces HTML rules on the string. IE in particular will throw errors or misparse XML strings passed to $() in this way.</em></p></blockquote>
<p>Furthermore, the second you try to pass any sizable XML or HTML string to jQuery&#8217;s constructor, Firefox 3 is going to fail with a &#8220;<em>script stack space quota is exhausted</em>&#8221; error (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=579656" target="_blank">see the bug report</a> filed with Mozilla).</p>
<p style="text-align: center;"><img src="/examples/xml/firefoxerror.png" alt="script stack space quota is exhausted" /></p>
<p>As Dave Methuin (core team member) <a href="http://dev.jquery.com/ticket/6796" target="_blank">explains</a>:</p>
<blockquote><p><em>Firefox seems to be dying on the constructor check for HTML, which uses a RegExp?:</em></p>
<p><em>match = quickExpr.exec( selector );</em></p>
<p><em>The string you&#8217;re passing in is long and html-ish, and I suspect Firefox  is backtracking heavily trying to match the huge string. We can either  assume a long (&gt;1K chars) string must be HTML or come up with a RegExp? that doesn&#8217;t need to backtrack so much.</em></p></blockquote>
<p>So how do you convert a string to an XML object?  It depends on your browser.<span id="more-886"></span></p>
<h2>Parse an XML string into a DOM</h2>
<p><em>The following only applies if you&#8217;re manually parsing an string.  If using XMLHttpRequest the browser does all this automatically &#8211; just use the responseXML property.</em></p>
<div class="update">
<strong>Update:</strong> the paragraph below is true if you&#8217;re using jQuery 1.4; however, you can now parse XML string with <code>$.parseXML()</code> in jQuery 1.5+.
</div>
<p>To re-iterate, jQuery alone is not capable of parsing XML strings. You will need to leverage the DOM parsing method baked into the browser, which all support in one form another.  Firefox, Chrome, and Safari have a <a href="https://developer.mozilla.org/En/DOMParser" target="_blank">DOMParser object</a>, and IE uses their proprietary ActiveX object.  To create a cross-browser solution:</p>
<p><script src="http://gist.github.com/553364.js?file=gistfile1.js"></script> This snippet checks for the absence of <code>DOMParser</code> in the window scope, and if true, defines it.  If the browser has an <code>ActiveXObject</code> (IE),  the <code>MLXML.DomDocument</code> method is used to convert the string to an DOM.  If <code>ActiveXObject</code> isn&#8217;t present but <code>XMLHttpRequest</code> is (fail-safe), a pseudo AJAX request is created and the parsed string is returned in the <code>responseXML</code> property of <code>XMLHttpRequest</code>.
<p>Now that we have a normalized <code>DOMParser</code>, parsing XML is easy:</p>
<p>    <script src="http://gist.github.com/554152.js?file=gistfile1.js"></script></p>
<h2>Converting an XML DOM back to a string</h2>
<p>Again, IE has a different implementation than everyone else, but it&#8217;s still trivial to normalize.  Firefox/Chrome/Safari use the <code>XMLSerializer.serializeToString</code> method, whereas IE has an &#8220;xml&#8221; property on the XML object.</p>
<p><script src="http://gist.github.com/554178.js?file=XMLSerializer.js"></script></p>
<h2>$.data() is unreliable</h2>
<p><img style="float: right; padding: 0 0 5px 5px;" src="/examples/xml/expando.png" alt="jQuery.expando" /></p>
<p>First a note on how <code>$.data</code> actually works.  When jQuery is loaded, <code>$.expando</code> is set to the current timestamp prefixed with &#8220;jQuery&#8221;.  Expando becomes the name of a property set on elements when data is added via <code>$.data</code>/<code>$.fn.data</code>.  The expando&#8217;s value is a UUID and acts as jQuery&#8217;s link back to the proper store in <code>$.cache</code> &#8211; where the data is actually stored.  Each key in the <code>$.cache</code> object is the UUID.</p>
<p>That said, the ActiveX object that IE uses for XML parsing has issue with jQuery trying to set expando properties on elements.  And instead of working around this issue, <a href="http://dev.jquery.com/ticket/6890" target="_blank">it appears</a> that the solution will be to document the fact that <code>$.data</code> doesn&#8217;t work cross-browser on XML documents.</p>
<p>Therefore, avoid setting data on XML documents.</p>
<h2>getElementById will always return null</h2>
<p>If part of your performance plan involves setting id attributes to leverage the speed of <code>getElementById</code>, think again. <code>getElementById</code> does not work on XML documents.</p>
<p>As documented in Mozilla&#8217;s <a href="https://developer.mozilla.org/en/document.getElementById" target="_blank">MDC</a>:</p>
<blockquote><p><em>DOM implementation must have information that says which attributes are of type ID. Attributes with the name &#8220;id&#8221; are not of type ID unless so defined in the document&#8217;s DTD. The id attribute is defined to be of ID type in the common cases of XHTML, XUL, and other. Implementations that do not know whether attributes are of type ID or not are expected to return null.</em></p></blockquote>
<p>&#8220;ID&#8221; is a data type, and in an XML document, the browser has no clue that an attribute named &#8220;id&#8221; should be interpreted as such. The same applies for the class attribute and <code>getElementsByClassName</code>, but have no fear; <code>getElementsByTagName</code> (and <code>querySelector</code>/<code>querySelectorAll</code> in modern browsers) will continue to work as expected.</p>
<p>jQuery&#8217;s Sizzle (and probably all other selector engines) account for this, so <code>$("#id")</code> will continue to work as expected.  Just don&#8217;t expect the same performance as <code>getElementById</code>.</p>
<p>The only benefit I see to passing raw XML to jQuery(), without parsing it into an XML DOM first, is the fact that <code>getElementById</code> will continue to work.  I still wouldn&#8217;t recommend it though.</p>
<h2>$.isXMLDoc()</h2>
<p>This little <a href="http://api.jquery.com/jQuery.isXMLDoc/" target="_blank">utility method</a> exists for testing whether or not a DOM node is within an XML document:</p>
<p><script src="http://gist.github.com/559144.js?file=isxmldoc.js"></script></p>
<p>Useful when you want to be certain you&#8217;re correctly passing an XML DOM to jQuery.</p>
<h2>No XML love in WebWorkers</h2>
<p>Initially, part of my performance strategy in this project was to leverage WebWorkers to avoid locking up the main UI thread.  After consulting the brilliance at <a href="http://bocoup.com" target="_blank">Bocoup</a>, <a href="http://twitter.com/rwaldron" target="_blank">Rick Waldron</a> noted that XHR objects used within workers will only return <code>responseText</code>, and never <code>responseXML</code>. This is by design as <code>responseXML</code> is not considered thread safe.  The <code>DOMparser</code> object is not available inside workers either, so unless you want to roll your own XML parser (hint: you don&#8217;t) do not try to request or parse XML.</p>
<h2>References</h2>
<p>Most of these links are scattered throughout this post, but if you want them all in one place:</p>
<ul>
<li style="margin-bottom: 10px;">jQuery ticket for &#8220;Error &#8216;script stack space quota is exhausted&#8217;&#8221; error<br />
<a href="http://dev.jquery.com/ticket/6796">http://dev.jquery.com/ticket/6796</a></li>
<li style="margin-bottom: 10px;">Mozilla ticket for &#8220;Error &#8216;script stack space quota is exhausted&#8217;&#8221;<br />
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=579656">https://bugzilla.mozilla.org/show_bug.cgi?id=579656</a></li>
<li style="margin-bottom: 10px;">jQuery ticket for $.data fails on XML documents in IE<br />
<a href="http://dev.jquery.com/ticket/6890">http://dev.jquery.com/ticket/6890</a></li>
<li style="margin-bottom: 10px;">MDC DOMParser Object<br />
<a href="https://developer.mozilla.org/en/DOMParser">https://developer.mozilla.org/en/DOMParser</a></li>
<li style="margin-bottom: 10px;">MDC XMLSerializer Object<br />
<a href="https://developer.mozilla.org/en/XMLSerializer">https://developer.mozilla.org/en/XMLSerializer</a></li>
<li style="margin-bottom: 10px;">MDC getElementById<br />
<a href="https://developer.mozilla.org/en/document.getElementById">https://developer.mozilla.org/en/document.getElementById</a></li>
<li style="margin-bottom: 10px;">jQuery&#8217;s constructor API documentation<br />
<a href="http://api.jquery.com/jQuery/#html">http://api.jquery.com/jQuery/</a></li>
<li style="margin-bottom:10px">$.isXMLDoc() API documentation<br /><a href="http://api.jquery.com/jQuery.isXMLDoc/">http://api.jquery.com/jQuery.isXMLDoc/</a></li>
<li style="margin-bottom:10px">Add cross-browser XML support to jQuery &#8211; feature enhancement ticket<br /><a href="http://dev.jquery.com/ticket/6693">http://dev.jquery.com/ticket/6693</a>
</li>
</ul>
<p>If you know of any other XML gotcha&#8217;s, please leave them in the comments below!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/working-with-xml-jquery-and-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery UI MultiSelect Widget</title>
		<link>http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/</link>
		<comments>http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 22:53:03 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jquery ui]]></category>
		<category><![CDATA[multiselect]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=832</guid>
		<description><![CDATA[This is the successor and port of my original jQuery MultiSelect Plugin to a jQuery UI widget. While both will actively be maintained, I highly recommend you use this version over the plugin version. It has a more robust feature set, is faster, and is much more flexible. MultiSelect turns an ordinary HTML select control [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/' rel='bookmark' title='jQuery MultiSelect Plugin w/ ThemeRoller Support'>jQuery MultiSelect Plugin w/ ThemeRoller Support</a></li>
<li><a href='http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/' rel='bookmark' title='Using &lt;code&gt;$.widget.bridge&lt;/code&gt; Outside of the Widget Factory'>Using <code>$.widget.bridge</code> Outside of the Widget Factory</a></li>
<li><a href='http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/' rel='bookmark' title='A jQuery UI Growl/Ubuntu-like Notification Widget'>A jQuery UI Growl/Ubuntu-like Notification Widget</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fjquery-ui-multiselect-widget%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fjquery-ui-multiselect-widget%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This is the successor and port of my original <a href="/jquery/jquery-multiselect-plugin-with-themeroller-support/">jQuery MultiSelect Plugin</a> to a jQuery UI widget.  While both will actively be maintained, I highly recommend you use this version over the plugin version.  It has a more robust feature set, is faster, and is much more flexible.  MultiSelect turns an ordinary HTML select control into an elegant drop down list of checkboxes with themeroller support.</p>
<p>This version inherits all the benefits from the jQuery UI widget factory that are not available in the plugin version.  The most requested feature was the ability to call methods on instances <em>after</em> initialization (e.g., statefullness), and now there are 10 to choose from!  Also present are eight events you can bind to, which in the previous version, had fewer and limited support.  Finally, there is support for effects.  Just include the jQuery UI effects dependency and specify the name of the opening or closing effect to use (and speed, if you wish!)</p>
<ul>
<li>Current version: 1.12 (11/26/2011 &#8211; <a href="http://github.com/ehynds/jquery-ui-multiselect-widget/raw/master/CHANGELOG" class="blank">changelog</a>)</li>
<li>View <a href="/examples/jquery-ui-multiselect-widget/demos/">demos</a>.</li>
<li>Download <a href="http://github.com/ehynds/jquery-ui-multiselect-widget/raw/1.12/src/jquery.multiselect.js">source</a> or <a href="http://github.com/ehynds/jquery-ui-multiselect-widget/raw/1.12/src/jquery.multiselect.min.js">minified</a>, and the <a href="http://github.com/ehynds/jquery-ui-multiselect-widget/raw/1.12/jquery.multiselect.css">CSS file</a>.</li>
<li><a href="http://github.com/ehynds/jquery-ui-multiselect-widget">Follow this project on GitHub</a>.</li>
<li>Run the <a href="/examples/jquery-ui-multiselect-widget/tests/unit/">unit tests</a>.</li>
<li>Please report any bugs on the <a href="http://github.com/ehynds/jquery-ui-multiselect-widget/issues">issue tracker</a>.
</li>
</ul>
<h2>Demo</h2>
<p>See what you&#8217;re missing out on?  Many more demos are available <a href="/examples/jquery-ui-multiselect-widget/demos/">here</a>.</p>
<select id="multiselect-demo" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
<option value="7">Option 7</option>
<option value="8">Option 8</option>
<option value="9">Option 9</option>
<option value="10">Option 10</option>
</select>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect-demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   selectedText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;# of # selected&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Advanced usage using optgroups and the filtering extension:</p>
<select id="multiselect-advanced" multiple="multiple" style="width:375px">
<optgroup label="Car Manufacturers"></p>
<option value="toyota">Toyota</option>
<option value="chevrolet">Chevrolet</option>
<option value="lexus">Lexus</option>
<option value="saturn">Saturn</option>
<option value="ford">Ford</option>
<option value="mazda">Mazda</option>
<option value="honda">Honda</option>
<option value="Nissan">Nissan</option>
<option value="pontiac">Pontiac</option>
<option value="GMC">GMC</option>
<p></optgroup><br />
<optgroup label="Boat Manufacturers"></p>
<option value="whaler">Boston Whaler</option>
<option value="grady">Grady White</option>
<option value="lund">Lund</option>
<option value="bayliner">Bayliuner</option>
<option value="panga">Panga</option>
<p></optgroup><br />
</select>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect-demo&quot;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      noneSelectedText<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Select car/boat manufacturers'</span>
      selectedList<span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span>
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">multiselectfilter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="/examples/jquery-ui-multiselect-widget/jquery.multiselect.css" />
<link rel="stylesheet" type="text/css" href="/examples/jquery-ui-multiselect-widget/jquery.multiselect.filter.css" /><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script><script type="text/javascript" src="/examples/jquery-ui-multiselect-widget/src/jquery.multiselect.js"></script><script type="text/javascript" src="/examples/jquery-ui-multiselect-widget/src/jquery.multiselect.filter.js"></script><script type="text/javascript">$("#multiselect-demo").multiselect({ selectedText:"# of # selected" });$("#multiselect-advanced").multiselect({ selectedList:4,noneSelectedText: 'Select car/boat manufacturers' }).multiselectfilter();</script></p>
<style>.ui-multiselect-checkboxes { text-align: left !important; }</style>
<p><span id="more-832"></span></p>
<h2>Usage</h2>
<p>Using this widget is simple.  First, include the following files:</p>
<ul>
<li>jQuery 1.4.2+</li>
<li>jQuery UI 1.8 widget factory and effects (if you&#8217;d like to use them)</li>
<li>A jQuery UI theme</li>
<li>This widget: jquery.multiselect.js</li>
<li>The CSS file: jquery.multiselect.css</li>
</ul>
<p>Next construct a standard multiple select box. Do not forget the <code>multiple</code> attribute:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multiple&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>Option 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>Option 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span>&gt;</span>Option 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;4&quot;</span>&gt;</span>Option 4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;5&quot;</span>&gt;</span>Option 5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>

<p>Finally, initialize the widget on the select box once the document is ready:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#example&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>See the <a href="/examples/jquery-ui-multiselect-widget/demos/">demos</a> for advanced usages and for more documentation!</p>
<h2>Options</h2>
<p>To further customize multiselect, pass in a object with one or more of the options below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// example:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   header<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Choose an Option!&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<table class="parameters" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:130px">Option</th>
<th>Description</th>
<th style="width:100px">Default</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">header</td>
<td>Either a boolean value denoting whether or not to display the header, or a string value.  If you pass a string, the default &#8220;check all&#8221;, &#8220;uncheck all&#8221;, and &#8220;close&#8221; links will be replaced with the specified text.</td>
<td>true</td>
</tr>
<tr>
<td class="parameter">height</td>
<td>Height of the checkbox container (scroll area) in pixels.  If set to &#8220;auto&#8221;, the height will calculate based on the number of checkboxes in the menu.</td>
<td>175</td>
</tr>
<tr>
<td class="parameter">minWidth</td>
<td>Minimum width of the entire widget in pixels.  Setting to &#8220;auto&#8221; will disable.</td>
<td>225</td>
</tr>
<tr>
<td class="parameter">checkAllText</td>
<td>The text of the &#8220;check all&#8221; link.</td>
<td>Check all</td>
</tr>
<tr>
<td class="parameter">uncheckAllText</td>
<td>The text of the &#8220;uncheck all&#8221; link.</td>
<td>Uncheck All</td>
</tr>
<tr>
<td class="parameter">noneSelectedText</td>
<td>The default text the select box when no options have been selected.</td>
<td>Select options</td>
</tr>
<tr>
<td class="parameter">selectedText</td>
<td>
			The text to display in the select box when options are selected (if <code>selectedList</code> is false). A pound sign (#) will automatically replaced by the number of checkboxes selected. If two pound signs are present in this parameter, the second will be replaced by the total number of checkboxes available. Example: &quot;# of # checked&quot;.</p>
<p>			This parameter also accepts an anonymous function with three arguments: the number of checkboxes checked, the total number of checkboxes, and an array of the checked checkbox DOM elements. See examples for usage.
		</td>
<td># selected</td>
</tr>
<tr>
<td class="parameter">selectedList</td>
<td>A numeric (or boolean to disable) value denoting whether or not to display the checked opens in a list, and how many. A number greater than 0 denotes the maximum number of list items to display before switching over to the selectedText parameter. A value of 0 or false is disabled.</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">show</td>
<td>The name of the effect to use when the menu opens.  To control the speed as well, pass in an array: <code>['slide', 500]</code></td>
<td>empty string</td>
</tr>
<tr>
<td class="parameter">hide</td>
<td>The name of the effect to use when the menu closes.  To control the speed as well, pass in an array: <code>['explode', 500]</code></td>
<td>empty string</td>
</tr>
<tr>
<td class="parameter">autoOpen</td>
<td>A boolean value denoting whether or not to automatically open the menu when the widget is initialized.</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">multiple</td>
<td>If set to false, the widget will use radio buttons instead of checkboxes, forcing users to select only one option.</td>
<td>true</td>
</tr>
<tr>
<td class="parameter">classes</td>
<td>
<div class="update"><strong>New in 1.5!</strong></div>
<p>Additional class(es) to apply to BOTH the button and menu for further customization. Separate multiple classes<br />
			with a space.  You&#8217;ll need to scope your CSS to differentiate between the button/menu:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* button */</span>
<span style="color: #6666ff;">.ui-multiselect</span><span style="color: #6666ff;">.myClass</span> <span style="color: #00AA00;">&#123;</span><span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* menu */</span>
<span style="color: #6666ff;">.ui-multiselect-menu</span><span style="color: #6666ff;">.myClass</span> <span style="color: #00AA00;">&#123;</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

</td>
<td>empty string</td>
</tr>
<tr>
<td class="parameter">position</td>
<td>
<div class="update"><strong>New in 1.5! Requires jQuery 1.4.3+, jQuery UI position utility</strong></div>
<p>This option allows you to position the menu anywhere you&#8217;d like relative to the button; centered, above, below (default), etc.  Also provides collision detection to flip the menu above the button when near the bottom of the window.  If you do not set this option or if the position utility has not been included, the menu will open below the button.</p>
<p>Requires the jQuery UI <a href="http://jqueryui.com/demos/position/">position utility</a> and jQuery 1.4.3+.  Please see <a href="/examples/jquery-ui-multiselect-widget/demos/#position">this demo</a> for usage instructions.</p>
</td>
<td>empty object</td>
</tr>
</tbody>
</table>
<h2>Events</h2>
<p>Hook into any of the events below by either binding to the event name, or passing in the name of the event during initialization:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// bind to event</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;multiselectopen&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// event handler here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or pass in the handler during initialization</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">open</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: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// event handler here</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<table class="parameters" cellspacing="0" cellpadding="0" style="width:600px">
<thead>
<tr>
<th style="width:130px">Event</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">create</td>
<td>
<div class="update"><strong>Requires jQuery UI Widget Factory 1.8.6+</strong></div>
<p>Fires when the widget is created for the first time.
</td>
</tr>
<tr>
<td class="parameter">beforeopen</td>
<td>Fires right before the menu opens.  Prevent the menu from opening by returning false in the handler.</td>
</tr>
<tr>
<td class="parameter">open</td>
<td>Fires after the widget opens.</td>
</tr>
<tr>
<td class="parameter">beforeclose</td>
<td>Fires right before the menu closes.  Prevent the menu from closing by returning false in the handler.</td>
</tr>
<tr>
<td class="parameter">close</td>
<td>Fires after the widget closes.</td>
</tr>
<tr>
<td class="parameter">checkAll</td>
<td>Fires when all the options are checked by either clicking the &#8220;check all&#8221; link in the header, or when the &#8220;checkall&#8221; method is programatically called (see next section).</td>
</tr>
<tr>
<td class="parameter">uncheckAll</td>
<td>Fires when all the options are all unchecked by either clicking the &#8220;uncheck all&#8221; link in the header, or when the &#8220;uncheckall&#8221; method is programatically called (see next section).</td>
</tr>
<tr>
<td class="parameter">optgrouptoggle</td>
<td>Fires when an optgroup label is clicked on.  This event receives the original event object as the first argument, and a hash of values as the second argument:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;multiselectoptgrouptoggle&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">/*
	event: the original event object, most likely &quot;click&quot;
&nbsp;
	ui.inputs: an array of the checkboxes (DOM elements)
        inside the optgroup
&nbsp;
        ui.label: the text of the optgroup
&nbsp;
        ui.checked: whether or not the checkboxes were
        checked or unchecked in the toggle (boolean)
	*/</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
<tr>
<td class="parameter">click</td>
<td>Fires when a checkbox is checked or unchecked.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;multiselectclick&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">/*
	event: the original event object
&nbsp;
	ui.value: value of the checkbox
	ui.text: text of the checkbox
	ui.checked: whether or not the input was checked
        or unchecked (boolean)
	*/</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<h2>Methods</h2>
<p>After an instance has been initialized, interact with it by calling any of these methods:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// example:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#multiselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;method_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<table class="parameters" cellspacing="0" cellpadding="0" style="width:600px">
<thead>
<tr>
<th style="width:130px">Method</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">open</td>
<td>Opens the menu.</td>
</tr>
<tr>
<td class="parameter">close</td>
<td>Closes the menu.</td>
</tr>
<tr>
<td class="parameter">refresh</td>
<td>
<div class="update"><strong>New in 1.8!</strong></div>
<p>Reloads the checkbox menu.  If you&#8217;re dynamically adding/removing option tags on the original select via AJAX or DOM manipulation methods, call <code>refresh</code> to reflect the changes in the widget.</td>
</tr>
<tr>
<td class="parameter">disable</td>
<td>Disable the entire widget.</td>
</tr>
<tr>
<td class="parameter">enable</td>
<td>Enable the entire widget.</td>
</tr>
<tr>
<td class="parameter">checkAll</td>
<td>Check all checkboxes.</td>
</tr>
<tr>
<td class="parameter">uncheckAll</td>
<td>Uncheck all checkboxes.</td>
</tr>
<tr>
<td class="parameter">isOpen</td>
<td>Returns a boolean denoting if the widget is currently open or not.</td>
</tr>
<tr>
<td class="parameter">getChecked</td>
<td>Returns an array of all the checked checkboxes.</td>
</tr>
<tr>
<td class="parameter">widget</td>
<td>Returns the menu container (all checkboxes inside).</td>
</tr>
<tr>
<td class="parameter">option</td>
<td>Set or get one of the options after the widget has been initialized.  If changing an option, the new option setting will take affect immediately.</td>
</tr>
<tr>
<td class="parameter">destroy</td>
<td>Destroy the widget, and revert back to the original select box.</td>
</tr>
</tbody>
</table>
<h2>Filter Plugin</h2>
<p>A filtering widget is available which, once initialized on a multiselect instance, will insert a text box inside the widget header.  Typing in the input will filter rows and return matches in real time.  To view a demo, download, or read the documentation, head to the <a href="/examples/jquery-ui-multiselect-widget/demos/index.htm#filter">demo page</a>.</p>
<h2>How do I&#8230;?</h2>
<p>These questions came out of the comments which others will probably find useful:</p>
<h3>Set default options for all multiselect instances?</h3>
<div style="margin-left:20px">
<p>The options object is located in <code>$.ech.multiselect.prototype.options</code>.  To configure options	that all new instances will inherit, it&#8217;s easier to set them in this object instead of on an instance-by-instance basis.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">ech</span>.<span style="color: #660066;">multiselect</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">selectedText</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;# of # selected&quot;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<h3>Retrieve all selected values?</h3>
<div style="margin-left:20px">
<p>The easiest way is to call <code>val()</code> on the select box:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The same can be accomplished using the multiselect API. Call the <code>getChecked</code> method and map a new array:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> array_of_checked_values <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;getChecked&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</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>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</div>
<h3>Retrieve values on the server?</h3>
<div style="margin-left:20px">
<p>Depends on your server-side language.  In PHP et. al., you may need to name your select with square brackets on the end so the values can be captured as an array.  I will probably wind up implementing this as an option at some point so it&#8217;ll degrade a bit better.</p>
</div>
<h3>Prevent the <code>selectedList</code> option from increasing the button&#8217;s height?</h3>
<div style="margin-left:20px">
<p>Open up the CSS file and edit the .ui-multiselect declaration.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.ui-multiselect</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">25px</span><span style="color: #00AA00;">;</span> overflow-x<span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">2px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">2px</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The height you&#8217;ll need to set depends on the font size and padding you use, so it may need adjusting.</p>
</div>
<h3>Manually check or uncheck a checkbox?</h3>
<div style="margin-left:20px">
<p>The checkboxes can be accessed after calling the &quot;widget&quot; method.  Simply manually trigger the NATIVE click event on them:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// manually check (or uncheck) the third checkbox in the menu:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiselect</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;widget&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:checkbox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</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>
   <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The native click event must be used (<code>trigger('click')</code> will not work) due to <a href="http://bugs.jquery.com/ticket/3827">this bug</a> in jQuery&#8217;s core.</p>
<p>All necessary events and actions, like updating the button value, will automatically fire.</p>
<p>Alternatively, you could give the original <code>option</code> tag the <code>selected</code> attribute, and then call MultiSelect&#8217;s <code>refresh</code> method.</p>
</div>
<h3>Show checked values on another part of my page?</h3>
<div style="margin-left:20px">
<p>See this demo: <a href="http://jsfiddle.net/ehynds/rTjkr/">http://jsfiddle.net/ehynds/rTjkr/</a></p>
</div>
<h3>Use multiselect in conjunction with the validate plugin?</h3>
<div style="margin-left:20px">
Make sure you&#8217;re using version 1.7 of the validation plugin; 1.6 is NOT supported with either this plugin or jQuery version 1.4.2+.  Validate 1.6 defines its own &#8220;delegate&#8221; method which conflicts with the method added in jQuery.  The conflict was resolved in Validate 1.7.
</div>
<h2>Issues</h2>
<p>If you find any issues, please report them using the <a href="http://github.com/ehynds/jquery-ui-multiselect-widget/issues" class="blank">GitHub issue tracker</a>.  Thanks!</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/' rel='bookmark' title='jQuery MultiSelect Plugin w/ ThemeRoller Support'>jQuery MultiSelect Plugin w/ ThemeRoller Support</a></li>
<li><a href='http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/' rel='bookmark' title='Using &lt;code&gt;$.widget.bridge&lt;/code&gt; Outside of the Widget Factory'>Using <code>$.widget.bridge</code> Outside of the Widget Factory</a></li>
<li><a href='http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/' rel='bookmark' title='A jQuery UI Growl/Ubuntu-like Notification Widget'>A jQuery UI Growl/Ubuntu-like Notification Widget</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/feed/</wfw:commentRss>
		<slash:comments>498</slash:comments>
		</item>
		<item>
		<title>jQuery &#8220;Create&#8221; Event</title>
		<link>http://www.erichynds.com/jquery/jquery-create-event/</link>
		<comments>http://www.erichynds.com/jquery/jquery-create-event/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 12:12:47 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[livequery]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=725</guid>
		<description><![CDATA[The livequery plugin allows us to bind events and general logic to all current and future elements. After the inclusion of live() in 1.3, an expansion of supported live event types in 1.4, and the introduction of delegate() in 1.4.2, the livequery plugin has more or less become deprecated. However, it still holds a special [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fjquery-create-event%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fjquery-create-event%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>The <a href="http://brandonaaron.net/code/livequery/docs" class="blank">livequery</a> plugin allows us to bind events and general logic to all current and future elements.  After the inclusion of <code>live()</code> in 1.3, an expansion of supported live event types in 1.4, and the introduction of delegate() in 1.4.2, the livequery plugin has more or less become deprecated.  However, it still holds a special place in my heart because of it&#8217;s ability to listen for the creation of new elements.</p>
<p>Listening for new elements is most useful when you want to apply logic, like initializing a plugin, to elements that will be added via AJAX.  Without livequery, this is easily accomplished by re-binding logic at the point where elements are injected:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// apply a plugin to all current div.foo's</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">somePlugin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// do some ajax</span>
$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bar.htm&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> response <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// bind the plugin to the new element, and insert it into the DOM</span>
	$<span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">somePlugin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you have multiple points of entry, however, this syntax can quickly become hard to maintain and is not very DRY.  Livequery allows you to bind it once and forget about it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// apply somePlugin to all current and future div.foo's</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</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>
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">somePlugin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// do some ajax</span>
$.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bar.htm&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> response <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span>body<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span> response <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// somePlugin will be applied to any additional </span>
	<span style="color: #006600; font-style: italic;">// div.foo's present in &quot;response&quot; automagically</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To me the appearance of a new element always seems like an event, and it would be cool if this was something you could bind to.  The idea here is that once jQuery injects an element into the DOM, any handlers attached to the element&#8217;s selector would fire.  Luckily, most of jQuery&#8217;s DOM manipulation methods (append, prepend, after, before, etc.) eventually funnel down into the <code>$.fn.domManip</code> method.  With this in mind, I <a href="http://paulirish.com/2010/duck-punching-with-jquery/" class="blank">duck punched</a> <code>$.fn.domManip</code>, added a new &#8220;create&#8221; special event, making this type of syntax possible:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// when a new div.foo element enters the DOM, call somePlugin() on it.</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&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>
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">somePlugin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-725"></span></p>
<p>To see a demo of this, <a href="/examples/jquery-create-event/">click here</a>.  The only manipulation method <code>$.fn.domManip</code> does not cover is <code>$.fn.html</code>, so this method is hijacked as well. </p>
<p>And the code (<a href="http://github.com/ehynds/jquery-create-event" class="blank">download</a> on GitHub):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// version 1.3 - 07/03/2010</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #339933;">,</span> _domManip<span style="color: #339933;">,</span> _html<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> selectors <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> gen <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> guid <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> old <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
   $.<span style="color: #660066;">event</span>.<span style="color: #660066;">special</span>.<span style="color: #660066;">create</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
      add<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> data <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         selectors.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> data.<span style="color: #660066;">selector</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// won't fire in 1.4.2 http://dev.jquery.com/ticket/6202</span>
      remove<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> data <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #003366; font-weight: bold;">var</span> len <span style="color: #339933;">=</span> selectors.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span> len<span style="color: #339933;">--</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> selectors<span style="color: #009900;">&#91;</span>len<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> data.<span style="color: #660066;">selector</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               selectors.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>len<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// deal with 99% of DOM manip methods</span>
   $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">domManip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> args<span style="color: #339933;">,</span> table<span style="color: #339933;">,</span> callback <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// if no create events are bound, just fire the original domManip method </span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>selectors.<span style="color: #660066;">length</span> <span style="color: #339933;">||</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</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>
         <span style="color: #000066; font-weight: bold;">return</span> _domManip.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">return</span> logic.<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> _domManip<span style="color: #339933;">,</span> arguments <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// deal with the remaining 1% (html method)</span>
   $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">html</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> value <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// if no create events are bound, html() is being called as a setter,</span>
      <span style="color: #006600; font-style: italic;">// or the value is a function, fire the original and peace out.  only string values use innerHTML;</span>
      <span style="color: #006600; font-style: italic;">// function values use append() which is covered by $.fn.domManip </span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>selectors.<span style="color: #660066;">length</span> <span style="color: #339933;">||</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> value <span style="color: #339933;">===</span> undefined <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">return</span> _html.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// make value an array</span>
      arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> logic.<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> _html<span style="color: #339933;">,</span> arguments <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #003366; font-weight: bold;">function</span> logic<span style="color: #009900;">&#40;</span> method<span style="color: #339933;">,</span> args <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> node<span style="color: #339933;">,</span> nodes <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> html <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> numSelectors <span style="color: #339933;">=</span> selectors.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> matches <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// crawl through the html structure passed in looking for matching elements.</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>nodes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         node <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>nodes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> walk<span style="color: #009900;">&#40;</span> element <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            element <span style="color: #339933;">=</span> element <span style="color: #339933;">||</span> node<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">parentNode</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> cur <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>element <span style="color: #339933;">?</span> element.<span style="color: #660066;">firstChild</span> <span style="color: #339933;">:</span> node<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>cur <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x<span style="color: #339933;">&lt;</span>numSelectors<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span>cur<span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span>selectors<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>cur.<span style="color: #660066;">id</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        cur.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;jqcreateevt&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>guid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        gen.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>cur.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// remember that this ID was generated</span>
                     <span style="color: #009900;">&#125;</span>
&nbsp;
                     <span style="color: #006600; font-style: italic;">// remember this match</span>
                     matches.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>cur.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
&nbsp;
               walk<span style="color: #009900;">&#40;</span> cur <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               cur <span style="color: #339933;">=</span> cur.<span style="color: #660066;">nextSibling</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #006600; font-style: italic;">// the html we started with, but with ids attached to elements</span>
         <span style="color: #006600; font-style: italic;">// bound with create.</span>
         html <span style="color: #339933;">=</span> html.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span> node <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// overwrite the passed in html with the new html</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> html<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// inject elems into DOM</span>
      <span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> method.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// for elements with a create event...</span>
      $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>matches<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span> id <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> elem <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// cleanup generated IDs</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">inArray</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> gen<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               elem.<span style="color: #660066;">removeAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// double check to make sure the event hasn't already fired.</span>
            <span style="color: #006600; font-style: italic;">// can happen with wrap()</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>$.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span> elem<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;jqcreateevt&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                $.<span style="color: #660066;">event</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               $.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;jqcreateevt&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #339933;">,</span> jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">domManip</span><span style="color: #339933;">,</span> jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In a nutshell, I&#8217;m creating a new special event called &#8220;create&#8221;, which simply stores the selector used when binding the event into an array.  Next, I hijack both <code>$.fn.domManip</code> and <code>$.fn.html</code> methods to walk through any HTML passed in looking for elements with a bound create event.  If any matches are found, each element is given a unique ID (if one does not already exist), stored in the <code>matches</code> array, and the original <code>$.fn.domManip</code> method is called to actually inject the HTML into the DOM.  Now that the nodes exist in the DOM and not just in a variable, I loop through the <code>matches</code> array, find each element with <code>getElementById</code>, and the trigger the create event.  If the element had to be assigned a unique ID it is removed.</p>
<h2>Usage Notes</h2>
<p>A few notes on how to use this thing/how it works:</p>
<ul>
<li>The event must be bound with live() or delegate() because of the way these methods hold onto the <code>this.selector</code> property.  This code uses <code>this.selector</code> to determine if injected elements match the elements bound to create.</li>
<li>If you attempt to do any DOM manipulation with <code>$(this)</code> inside the event handler, you&#8217;ll trigger infinite recursion.</li>
<li>Elements must enter the DOM through one of jQuery&#8217;s DOM manipulation functions, or a method that calls it (append, prepend, after, etc.).  It won&#8217;t work using vanilla JavaScript.</li>
<li>Don&#8217;t forget that you can <code>return false</code> to prevent this event from bubbling.</li>
<li>If no &#8220;create&#8221; events have been bound, <code>$.fn.domManip</code> or <code>$.fn.html</code> is called straight up without the extra duck punched logic.  However, once all &#8220;create&#8221; events have been removed (via <code>die()</code>), this plugin won&#8217;t have any idea because of <a href="http://dev.jquery.com/ticket/6202" class="blank">this bug</a> in 1.4.2.</li>
<li>Cross browser compatible (including IE6)!</li>
</ul>
<h2>Download/Demo</h2>
<p><a href="http://github.com/ehynds/jquery-create-event" class="blank">Download</a> on Github and <a href="/examples/jquery-create-event">view the demos</a>.  Unit tests are <a href="/examples/jquery-create-event/tests">here</a>.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-create-event/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>A jQuery UI Growl/Ubuntu-like Notification Widget</title>
		<link>http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/</link>
		<comments>http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/#comments</comments>
		<pubDate>Thu, 13 May 2010 00:11:32 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[growl]]></category>
		<category><![CDATA[jquery ui]]></category>
		<category><![CDATA[notification]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=664</guid>
		<description><![CDATA[Update 7/6/2010: version 1.4.1 is out! There are about a dozen other plugins out there that do this already, except most seem to come with an enormous footprint: 10-12k of code, X-number of images, and roughly 1000 options to support every plausible use case. For other minimalists like myself out there, here&#8217;s one built off [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/' rel='bookmark' title='Using &lt;code&gt;$.widget.bridge&lt;/code&gt; Outside of the Widget Factory'>Using <code>$.widget.bridge</code> Outside of the Widget Factory</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/' rel='bookmark' title='jQuery UI MultiSelect Widget'>jQuery UI MultiSelect Widget</a></li>
<li><a href='http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/' rel='bookmark' title='Tips for Developing jQuery UI 1.8 Widgets'>Tips for Developing jQuery UI 1.8 Widgets</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fa-jquery-ui-growl-ubuntu-notification-widget%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fa-jquery-ui-growl-ubuntu-notification-widget%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="update" style="margin:15px 5px 0 0"><strong>Update 7/6/2010:</strong> version 1.4.1 is out!</div>
<p>There are about a dozen other plugins out there that do this already, except most seem to come with an enormous footprint: 10-12k of code, X-number of images, and roughly 1000 options to support every plausible use case.  For other minimalists like myself out there, here&#8217;s one built off the jQuery UI widget factory in approx. 110 lines of code and 100% CSS.  In typical widget fashion, this implementation supports the most basic (and arguably most common) uses, but is flexible enough for more advanced cases.</p>
<div style="overflow:hidden;zoom:1">
<img src="/examples/jquery-notify/example.gif" alt="" style="float:right" /></p>
<ul>
<li><a href="/examples/jquery-notify">View demos</a></li>
<li><a href="http://github.com/ehynds/jquery-notify">Follow on GitHub</a></li>
<li>Download <a href="http://github.com/ehynds/jquery-notify/raw/master/src/jquery.notify.js">source</a> and <a href="http://github.com/ehynds/jquery-notify/raw/master/ui.notify.css">css file</a></li>
<li><a href="#faq">FAQ</a></li>
</ul>
</div>
<p>Internet Explorer is a big-time downer with no HSLA/border-radius/box-shadow support, so if eye candy is important to you in IE, a transparent png background or similar can be added without much effort.<span id="more-664"></span></p>
<h2>Usage</h2>
<p>Using this widget is a simple four step process:</p>
<h3>Step 1:</h3>
<p>Include the JavaScript and CSS dependencies.  Only the widget factory from jQuery UI is required.</p>
<ol>
<li>CSS file: <a href="http://github.com/ehynds/jquery-notify/raw/master/ui.notify.css">ui.notify.css</a></li>
<li>jQuery and the jQuery UI widget factory: ui.widget.js</li>
<li>This widget: <a href="http://github.com/ehynds/jquery-notify/raw/master/src/jquery.notify.js">ui.notify.js</a></li>
</ol>
<h3>Step 2:</h3>
<p>Create a container element to hold notifications, and a template from which all notifications will be constructed from. With this, you can have multiple containers on the same page holding different styles of notifications.  Each container can contain different templates.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- set the container hidden to avoid a flash of unstyled content</span>
<span style="color: #808080; font-style: italic;">when the page first loads --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none&quot;</span>&gt;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- </span>
<span style="color: #808080; font-style: italic;">    Later on, you can choose which template to use by referring to the </span>
<span style="color: #808080; font-style: italic;">    ID assigned to each template.  Alternatively, you could refer</span>
<span style="color: #808080; font-style: italic;">    to each template by index, so in this example, &quot;basic-tempate&quot; is</span>
<span style="color: #808080; font-style: italic;">    index 0 and &quot;advanced-template&quot; is index 1.</span>
<span style="color: #808080; font-style: italic;">    --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;basic-template&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ui-notify-cross ui-notify-close&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>x<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>#{title}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>#{text}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;advanced-template&quot;</span>&gt;</span>
        <span style="color: #808080; font-style: italic;">&lt;!-- ... you get the idea ... --&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Once the widget is initialized on the container, each template is cached and removed from the DOM. You can add one or more close links in your template by assigning each anchor the class <code>ui-notify-close</code>.  The close link in this example has additional styling through the class <code>ui-notify-cross</code>, which makes the &#8220;x&#8221; look like an icon. </p>
<p>Define any variables you want to include in this template using <code>#{varname}</code> syntax. You can call these anything you&#8217;d like.</p>
<h3>Step 3</h3>
<p>Initiate the widget on the container, optionally passing in a hash of default options:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// basic</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or with options (there are only 2)</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    speed<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
    expires<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Step 4</h3>
<p>Notifications are actually displayed by calling the <code>create</code> method.  Pass in an hash of variables to transpose into the template:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// notice the templates in step 2 are expecting title and text variables.</span>
<span style="color: #006600; font-style: italic;">// you can add as many as you like, and call them whatever you want.</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Test Notification'</span><span style="color: #339933;">,</span>
    text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'This is an example of the default config, and will fade out after five seconds.'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you&#8217;d like, set specific options for each notification by passing in a second hash:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// example of overriding the expires and speed options for one notification</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Test Notification'</span><span style="color: #339933;">,</span>
    text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'This is an example of the default config, and will fade out after five seconds.'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
    expires<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    speed<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can specify the template to use by passing in the ID of the template as the second parameter.  This is optional, and if not provided the first template defined will be used.  Alternatively, you can pass in the index of the template in the container as the second argument.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create a new notification using the &quot;basic&quot; template</span>
<span style="color: #006600; font-style: italic;">// that we defined in step 2.</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;basic-template&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* template vars */</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// we can refer to the template by index as well.  this</span>
<span style="color: #006600; font-style: italic;">// statement is equivalent:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* template vars */</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The create method returns a notification instance object with two public methods: <code>open</code> and <code>close</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create a new &quot;sticky&quot; notification</span>
<span style="color: #003366; font-weight: bold;">var</span> instance <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> sticky<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// close it</span>
instance.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// re-open it</span>
instance.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Options</h2>
<p>These options can be set for all notifications within a container, or on a notification-by-notification basis:</p>
<table class="parameters" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:130px">Parameter</th>
<th>Description</th>
<th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">speed</td>
<td>The amount of time in milliseconds to fade notifications in and out.</td>
<td>500</td>
</tr>
<tr>
<td class="parameter">expires</td>
<td>Notifications will automatically close after this amount of time, in milliseconds.  Set to 0 or <code>false</code> to create a &#8220;sticky&#8221; notifications.</td>
<td>5000</td>
</tr>
<tr>
<td class="parameter">stack</td>
<td>
<p class="update">New in version 1.2.1!</p>
<p>			Notifications will stack downwards if set to &#8220;below&#8221; (default), or upwards if set to &#8220;above.&#8221;
		</td>
<td>below</td>
</tr>
<tr>
<td class="parameter">custom</td>
<td>
<div class="update">New in version 1.3!</div>
<p>A boolean value denoting whether or not the widget should apply its own styling classes.  Set to false to roll your own notification themes (including ThemeRoller).  You could simply overwrite the included CSS, but using this option allows you to create default AND custom notifications within the same containers, as well as maintain a clear upgrade path as you won&#8217;t have to touch any of the included files.</td>
<td>false</td>
</tr>
</tbody>
</table>
<h2>Events</h2>
<p>These events can be bound by passing handlers in as options.  Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// for a specific notification</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#example&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* template vars */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">open</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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Notification opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or for all notifications within a container</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">open</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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Notification opened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<table class="parameters" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:130px">Event</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">beforeopen</td>
<td class="">Fires before the notification opens.  If <code>false</code> is returned inside this callback, the notification will not open.</td>
</tr>
<tr>
<td class="parameter">open</td>
<td class="">Fires after the notifcation opens.</td>
</tr>
<tr>
<td class="parameter">close</td>
<td class="">Fires after the notifcation closes.</td>
</tr>
<tr>
<td class="parameter">click</td>
<td class="">Fires if the user clicks anywhere in the notification itself (not on the close link(s), if present).  Useful if you want to close the notification or perform some other action once the user has acknowledged the notice.  The callback receives  two parameters as arguments: the event object, and the notification instance object.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
   title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Clickable Notification'</span><span style="color: #339933;">,</span>
   text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Click on me to fire a callback'</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>
   click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span>instance<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// close the notice if the user clicks anywhere inside it</span>
      instance.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<h2 id="faq">FAQ</h2>
<h3>How do I use ThemeRoller in my templates?</h3>
<p>First, create a template and apply the appropriate ThemeRoller classes, icon classes you want to use, etc.:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;notification-container&quot;</span>&gt;</span>
&nbsp;
   <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;themeroller&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ui-state-error&quot;</span>&gt;</span>
      <span style="color: #808080; font-style: italic;">&lt;!-- close link --&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ui-notify-close&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>
         <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ui-icon ui-icon-close&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:right&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;">&lt;!-- alert icon --&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;float:left; margin:2px 5px 0 0;&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ui-icon ui-icon-alert&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
&nbsp;
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>#{title}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>#{text}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
   <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;">&lt;!-- other templates here, if you'd like.. --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Next, either when you init the widget or create a notification, ensure the <code>custom</code> parameter is set to <code>false</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// prevent notify from imposing its own styling classes</span>
<span style="color: #006600; font-style: italic;">// on ALL notifications inside this container </span>
<span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#notification-container&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> custom<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span> text<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;bar&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or just themeroller templates</span>
<span style="color: #003366; font-weight: bold;">var</span> handler <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#notification-container&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;create&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;themeroller&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> title<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span> text<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;bar&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> custom<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h3 style="margin:15px 0">How do I add a close icon or link to my templates?</h3>
<p>Any link, button, etc. with the class <code>ui-notify-close</code> will close the notification when clicked.  To use the X close link as seen in the demos, apply the <code>ui-notify-cross</code> class as well:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ui-notify-close ui-notify-cross&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>x<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<h3 style="margin:15px 0">How do I avoid a FOUC (flash of unstyled content) when the page first loads?</h3>
<p>Simply give your container the <code>display: none;</code> CSS property.  Once you initialize this widget on the container, it&#8217;ll take care of the rest.</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/' rel='bookmark' title='Using &lt;code&gt;$.widget.bridge&lt;/code&gt; Outside of the Widget Factory'>Using <code>$.widget.bridge</code> Outside of the Widget Factory</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/' rel='bookmark' title='jQuery UI MultiSelect Widget'>jQuery UI MultiSelect Widget</a></li>
<li><a href='http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/' rel='bookmark' title='Tips for Developing jQuery UI 1.8 Widgets'>Tips for Developing jQuery UI 1.8 Widgets</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Tips for Developing jQuery UI 1.8 Widgets</title>
		<link>http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/</link>
		<comments>http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 12:33:32 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jquery ui]]></category>
		<category><![CDATA[widget factory]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=543</guid>
		<description><![CDATA[I&#8217;m wrapping up my first jQuery UI widget (see multiselect on GitHub) and thought it would be useful to share some notes I took on the widget factory &#038; widget development in general.  I personally found development on the factory to be quite enjoyable; a lot of functionality is available right out the box (custom events, [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/' rel='bookmark' title='Using &lt;code&gt;$.widget.bridge&lt;/code&gt; Outside of the Widget Factory'>Using <code>$.widget.bridge</code> Outside of the Widget Factory</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Ftips-for-developing-jquery-ui-widgets%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Ftips-for-developing-jquery-ui-widgets%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;m wrapping up my first jQuery UI widget (see <a class="blank" href="http://github.com/ehynds/jquery-ui-multiselect-widget">multiselect</a> on GitHub) and thought it would be useful to share some notes I took on the widget factory &#038; widget development in general.  I personally found development on the factory to be quite enjoyable; a lot of functionality is available right out the box (custom events, ability to change options after initialization) and idioms you might not otherwise consider, like widget destruction.  Furthermore, it imposes a clean object-literal development structure with public and private methods, making it much easier to start a project or maintain other&#8217;s projects.</p>
<p>Throughout this blog post I will be showing you various ways to modify the <a href="http://jqueryui.com/demos/dialog/" class="blank">jQuery UI dialog widget</a> source to add your own features.  I do not actually recommend doing this however, because widgets can just as easily be extended.  The concepts are the same though and are easily adaptable to your own project.  This post also assumes you are already familiar with the widget factory; if you aren&#8217;t, be sure to <a href="http://docs.jquery.com/UI_Developer_Guide#The_widget_factory" class="blank">familiarize yourself</a> first.</p>
<h2>_init() and _create()</h2>
<p>The widget factory automatically fires the <code>_create()</code> and <code>_init()</code> methods during initialization, in that order.  At first glance it appears that the effort is duplicated, but there is a sight difference between the two.  Because the widget factory protects against multiple instantiations on the same element,  <code>_create()</code> will be called a maximum of one time for each widget instance, whereas <code>_init()</code> will be called each time the widget is called without arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// _create() and _init() fire on the first call.</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mywidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// a widget has already been instantiated on the div, so this time</span>
	<span style="color: #006600; font-style: italic;">// only _init will fire.</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mywidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// however, once the widget is destroyed...</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mywidget</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;destroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// both _create() and _init() will fire.</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mywidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So how do you know where to place which logic?  Use <code>_create</code> to build &amp; inject markup, bind events, etc.  Place default functionality in <code>_init()</code>.  The dialog widget, for example, provides an <code>autoOpen</code> parameter denoting whether or not the dialog should be open once the widget is initialized; a perfect spot for <code>_init</code>!<span id="more-543"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js - autoOpen is true by default</span>
_init<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: #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;">options</span>.<span style="color: #660066;">autoOpen</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The best way to visualize the difference is to load up Firebug and navigate to the <a class="blank" href="http://jqueryui.com/demos/dialog/">UI dialog</a> demo page.  Because the <code>autoOpen</code> parameter is true by default, the dialog is created and opened when the page loads.  Close the dialog by clicking on the close link in the toolbar, open Firebug, and attempt to reinitialize the demo by running this in your console: <code>$("#dialog").dialog()</code>.  The widget factory knows there is already a dialog instance bound to the #dialog DIV; it just happens to be closed.  Therefore, the widget factory will only fire the <code>_init()</code> method, which as you can see above, will simply open the dialog.</p>
<h2>_trigger</h2>
<p>Custom events can be fired from within your widget by using the internal <code>_trigger</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// call the trigger method passing in the name of the event to fire,</span>
<span style="color: #006600; font-style: italic;">// and optionally an event and data object </span>
<span style="color: #000066; font-weight: bold;">this</span>._trigger<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;eventName&quot;</span><span style="color: #339933;">,</span> eventObj<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><code>_trigger</code> is provided by the widget factory, and is <strong>not</strong> the same as the jQuery <code>trigger()</code> method located in the $.fn namespace (notice the underscore prefix in the widget version).  Continuing with the dialog example, developers can execute logic when the dialog opens by either passing in an &#8220;open&#8221; callback, or later binding to the &#8220;dialogopen&#8221; event:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// provide an open callback during plugin creation</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">open</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: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// do stuff</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or bind to the dialogopen event</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dialogopen&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do stuff</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>When I was trying to integrate similar functionality into multiselect, I scoured the dialog&#8217;s source code looking for a trigger on the &#8220;dialogopen&#8221; event to see how it was done.  Said trigger does not exist.  Black magic, I thought, but as it turns out there is a call to <code>_trigger("open")</code> inside the dialog&#8217;s &#8220;open&#8221; method.  Success!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&nbsp;
<span style="color: #000066;">open</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: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// open logic here</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// event is fired </span>
	self._trigger<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'open'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When <code>self._trigger("eventName")</code> is called:</p>
<ol>
<li>The widget factory automatically prepends the widget&#8217;s name to the name of the event you want to trigger &#8211; and fires the event.</li>
<li>If there is a function with the name of the event you&#8217;re trying to trigger <strong>inside the options object</strong>, that function will be called as well.</li>
</ol>
<p>A number of other things go down &#8211; like copying the original event object if it exists &#8211; but for developers, the points above are the most important parts to remember.</p>
<h2>Preventing default actions with _trigger</h2>
<p>There are a number of events in jQuery UI where, if you bind to them and <code>return false</code>, the default behavior will not fire.  The &#8220;select&#8221; event in the Autocomplete widget, for example, fires when an item is selected from the menu.  From the UI docs:</p>
<blockquote><p>The default action of select is to replace the text field&#8217;s value with the value of the selected item. Canceling this event prevents the value from being updated, but does not prevent the menu from closing.</p></blockquote>
<p>Applying this to the dialog widget, one might expect that something like this will prevent a dialog from opening:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// no workie</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dialogopen&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>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is not supported with the open event, nor should it to be, because users expect this type of event to fire <em>after</em> the dialog has opened.  So let&#8217;s modify the source of the widget and add our own custom &#8220;beforeopen&#8221; event, which will fire immediately once the open method is called. Before the logic to actually display the dialog, and before the open event is fired:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&nbsp;
<span style="color: #000066;">open</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: #006600; font-style: italic;">// bail out of the open method if the returned value from the</span>
	<span style="color: #006600; font-style: italic;">// beforeopen event is false.</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._trigger<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'beforeopen'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// continue rest of dialog open logic.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we can bind to this new beforeopen event and prevent the dialog from opening simply by returning false:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dialogbeforeopen&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>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	beforeopen<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: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Huzzah!  On the same note, event callbacks take two optional parameters: the event object and an options object.  If an event was triggered by another event &#8211; like the &#8220;close&#8221; event being triggered by clicking on the close icon in the dialog&#8217;s title bar &#8211; the original click event has not been lost.  In this context, <code>event.type</code> equals &#8220;dialogclose&#8221;, but the &#8220;click&#8221; event is accessible under the <code>event.originalEvent</code> namespace:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">close</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: #339933;">,</span> ui<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; &quot;dialogClose&quot;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// when fired programatically: $(&quot;#dialog&quot;).dialog(&quot;close&quot;):</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> event.<span style="color: #660066;">originalEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; &quot;undefined&quot;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// but when called with the esc key or clicking on the</span>
		<span style="color: #006600; font-style: italic;">// close icon:</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> event.<span style="color: #660066;">originalEvent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; &quot;object&quot;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">originalEvent</span>.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// =&gt; &quot;click&quot;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Accessing other widget instances</h2>
<p>Let&#8217;s say you want to ensure that only one dialog can be open at any given time; that is, when a dialog&#8217;s open method is called, all other open dialog instances should be closed.  Unfortunately the widget factory does not hold an internal cache of widget instances, but this feature is trivial to implement none the less.</p>
<p>First, it is important to understand how a DOM element is related to the widget it was initialized it on.  Most (if not all widgets) follow this pattern:</p>
<ol>
<li>You, the developer, queries the DOM for an element and calls the widget you want to initiate.</li>
<li>The widget generates new markup based on that element and injects it into the DOM somewhere.  The entire widget instance is then stored in is the element&#8217;s data() cache.</li>
<li>Optionally, if it makes sense, the widget hides the original element, seemingly transforming the old element into a new widget.</li>
</ol>
<p>Let me hit you with that once more in case you missed it: the entire widget instance is stored in the element&#8217;s data() cache.  If you were to examine <code>$("#element").data()</code> in firebug, you would see an object with the name of the widget you created on that element.</p>
<p>Once <code>$("#mydiv").dialog()</code> is called, new markup is generated and injected into the DOM, and the original <code>mydiv</code> element is transformed into the content pane of the dialog.  Even through the <code>mydiv</code> element is now apart of the widget, it continues to be our link between the developer and the dialog instance:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// create a new dialog</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mydiv&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// #mydiv is still in the DOM, but is hidden.</span>
<span style="color: #006600; font-style: italic;">// it is our link back to the widget.  so, to interact with</span>
<span style="color: #006600; font-style: italic;">// the widget, we call a method on the original element:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mydiv&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or cache it in a var</span>
<span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mydiv&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This explains the relationship of a DOM element to it&#8217;s dialog instance, but in order to close all other dialogs, this relationship needs to be reversed.   All instances have to be found first, and then we access the underlying DOM element and perform the close method call.</p>
<p>You may be tempted to hack the dialog&#8217;s open method to find every element and aimlessly fire the dialog close method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// omg no - ui.dialog.js</span>
<span style="color: #000066;">open</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;">&#40;</span><span style="color: #3366CC;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// continue with open code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8230;which would work, but there is a better way.  Actually, there are <s>two</s>three better ways:</p>
<h3>Method 1: store the original element in the widget&#8217;s data cache</h3>
<p>I first came across this approach in the Filament Group&#8217;s <a href="http://www.filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/" class="blank">select widget</a>, so here&#8217;s a quick shout-out to them.  Since each dialog instance is stored in the element&#8217;s data cache, why not store the element inside the widget&#8217;s data cache?   This way we can easily query the DOM for all dialog widgets, access the underlying DOM element, and then fire the close method on those elements.  We&#8217;ll start by modifying the <code>_create</code> method to store the DOM element in the new widget:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&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>
	<span style="color: #006600; font-style: italic;">// all the original create code here.  </span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// the dialog's markup is generated and saved into </span>
	<span style="color: #006600; font-style: italic;">// this 'uiDialog' variable.</span>
	<span style="color: #003366; font-weight: bold;">var</span> uiDialog <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;div class=&quot;ui-dialog&quot;&gt; ... &lt;/div&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// store the original element (this.element) inside the </span>
	<span style="color: #006600; font-style: italic;">// widget's data store.</span>
	uiDialog.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;originalelement&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next, at the top of the &#8220;open&#8221; method, query the DOM for all dialog widgets (DIV elements with the class ui-dialog) and filter out the current instance.  It is necessary to exclude the current instance because otherwise, given two dialogs A and B where A is closed and B is open, opening A would trigger the close event for both A and B, when the event should only be fired on B.  Finally, loop through the matching ui-dialog DIV&#8217;s and grab the element it was originally created with out of the data store.  If the dialog is open, determined by calling the <code>dialog("isOpen")</code> method, then close it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
<span style="color: #000066;">open</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;">&#40;</span><span style="color: #3366CC;">&quot;div.ui-dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">uiDialog</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</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>
		<span style="color: #003366; font-weight: bold;">var</span> el <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>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;originalelement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>el.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;isOpen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			el.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// rest of open code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Method 2: maintain your own cache</h3>
<p>This method is certainly easier to understand, and involves extending the dialog widget to add a public property called &#8220;instances&#8221;.  When you define a widget with the <code>$.widget("namespace.widgetname")</code> syntax, the widget factory <em>instantiates</em> the widget &#8220;class&#8221; inside <code>$[ namespace ]</code>.  All jQuery UI widgets exist in the <code>$.ui</code> namespace, which is reserved for official widgets, so make sure you change this to something unique.  Namespaces are used internally for organizational purposes; they do <em>not</em> allow you to create multiple widgets with the same name.</p>
<p>With this in mind, <code>$.ui.dialog</code> can easily be extended to include an instances property, which will start out as an empty array.  Each time a dialog in initialized, the associated DOM element will be pushed into it, providing a clear path to all instances.  On the token, each instance will be removed from the array on destruction.</p>
<p>Towards the bottom of the <code>ui.dialog.js</code> file, extend <code>$.ui.dialog</code> to include an instances property:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&nbsp;
<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>
	$.<span style="color: #660066;">widget</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ui.dialog&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// core of widget code here</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// the dialog has some other code here</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// extend $.ui.dialog, adding a public &quot;instances&quot; property</span>
	$.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">ui</span>.<span style="color: #660066;">dialog</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
		instances<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, push the original DOM element onto the <code>$.ui.dialog.instances</code> array during initialization:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&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>
	$.<span style="color: #660066;">ui</span>.<span style="color: #660066;">dialog</span>.<span style="color: #660066;">instances</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// rest of _create code here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The advantage of this pattern is clear: all instances of the widget are accessible publicly AND from within the widget itself.  It is trivial to access all elements that have a particular widget bound to them from outside your widget:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// this line becomes valid from both inside and outside the widget</span>
<span style="color: #006600; font-style: italic;">// as soon as ui.dialog.js is loaded into your page:</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;There are currently &quot;</span> <span style="color: #339933;">+</span> $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">dialog</span>.<span style="color: #660066;">instances</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; dialog instances.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, in the open method of the dialog, use <a class="blank" href="http://api.jquery.com/jquery.grep/"><code>$.grep</code></a> to remove the current instance from <code>$.ui.dialog.instances</code>, saving these &#8220;other&#8221; instances into the variable <code>otherInstances</code>.  Loop through this new array, check if the dialog associated with it is open, and if so, call the close method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&nbsp;
<span style="color: #000066;">open</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: #006600; font-style: italic;">// the DOM element associated with this instance</span>
	<span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// go through each element in the array, returning a</span>
	<span style="color: #006600; font-style: italic;">// new array of instances excluding the current one</span>
	<span style="color: #003366; font-weight: bold;">var</span> otherInstances <span style="color: #339933;">=</span> $.<span style="color: #660066;">grep</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">ui</span>.<span style="color: #660066;">dialog</span>.<span style="color: #660066;">instances</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> elem <span style="color: #339933;">!==</span> element<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>otherInstances<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: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $this.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;isOpen&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$this.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// rest of open code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The final step involves cleaning up after ourselves.  Each time an instance is destroyed it needs to be removed from the array, trivial enough to implement with jQuery&#8217;s <code>$.inArray</code> method and JavaScript&#8217;s <code>Array.splice</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&nbsp;
destroy<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: #006600; font-style: italic;">// the DOM element associated with this instance</span>
	<span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// the index, or location of this instance in the instances array</span>
	<span style="color: #003366; font-weight: bold;">var</span> position <span style="color: #339933;">=</span> $.<span style="color: #660066;">inArray</span><span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> $.<span style="color: #660066;">ui</span>.<span style="color: #660066;">dialog</span>.<span style="color: #660066;">instances</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// if this instance was found, splice it off</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>position <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$.<span style="color: #660066;">ui</span>.<span style="color: #660066;">dialog</span>.<span style="color: #660066;">instances</span>.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>position<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// continue destroy logic	</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<div class="update"><strong>Update 04/22/2010:</strong> <a href="http://yayquery.com/" class="blank">Adam Sontag</a> pointed out a third method:</div>
<h3>Method 3: widget pseudo-selector</h3>
<p>In the #jquery IRC channel, <a href="http://yayquery.com/" class="blank">Adam Sontag</a> of the yayQuery fame noted an undocumented feature of the widget factory: automatic pseudo selector generation for all widgets.  With this, it is super simple to query the DOM for all widgets of a certain type:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// gimme all dialogs</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:ui-dialog&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The selector above returns an object of DOM elements each instance was created on.  No need to maintain your own cache or store the DOM element inside each widget.  Closing all other open dialog instances using this pseudo selector is trivial:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ui.dialog.js</span>
&nbsp;
<span style="color: #000066;">open</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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// close all open dialogs, excluding this instance</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:ui-dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</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>
		<span style="color: #003366; font-weight: bold;">var</span> $this <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$this.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;isOpen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$this.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// rest of open code here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Nice and easy.  Choose whichever you think works best for you.  Method #2 gives you immediate access to a public array of instances and does not rely on querying the DOM; however, method #3 could not be any simpler.</p>
<h2>Widget destruction &#038; progressive enhancement</h2>
<p>If your widget progressively enhances an element &#8211; that is, it transforms something that works into something that works better and with a candy coating &#8211; always keep in the back of your mind what will happen if it is later destroyed.  I cannot think of an appropriate example with the dialog widget, but <a class="blank" href="http://github.com/ehynds/jquery-ui-multiselect-widget">multiselect</a> fits the bill perfectly. It works like this:</p>
<ol>
<li>A standard HTML multiple select box exists in the DOM.</li>
<li><code>$("select").multiselect()</code> is called which hides the select box and injects new markup into the DOM.</li>
<li>Users now interact with the widget using checkboxes instead of option tags.</li>
</ol>
<p>Just as you would ensure that pre-selected option tags are checked by default when the widget is created, you also want to think in reverse.  If a user checks 5 boxes and the widget is then destroyed, one would expect the same 5 option to also be selected.  Therefore, when a checkbox receives the <code>checked="checked"</code> attribute, the associated option tag also receives the <code>selected="selected"</code> attribute, <em>even though it is hidden from view</em>.  Therefore, the widget can be destroyed and re-created without losing any action the user performed.</p>
<p>Honestly, I cannot think of a legitimate use case where a widget is destroyed.  The widget factory supports it, however, so you should too.</p>
<h2>Resources</h2>
<p>For more widget goodness:</p>
<ul>
<li>See a <a href="http://gist.github.com/374414" class="blank">sample widget template</a> using most of the conventions outlined above.</li>
<li>Widget factory 1.7.2 to 1.8 <a href="http://jqueryui.com/docs/Upgrade_Guide#Widget_Factory" class="blank">upgrade guide</a> and <a href="http://github.com/scottgonzalez/jquery-ui-1.8-widget-factory" class="blank">sample templates</a></li>
<li>Widget factory <a href="http://jqueryui.pbworks.com/Widget-factory" class="blank">development &amp; planning documentation</a></li>
<li>Widget factory <a href="http://docs.jquery.com/UI_Developer_Guide#The_widget_factory" class="blank">developers guide</a></li>
<li>More <a href="http://bililite.com/blog/understanding-jquery-ui-widgets-a-tutorial/" class="blank">blog</a> <a href="http://net.tutsplus.com/tutorials/javascript-ajax/coding-your-first-jquery-ui-plugin/" class="blank">posts</a> about widget development</li>
</ul>
<p>If you have any feedback, questions, or other useful tips, please leave them in the comments section below.</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/using-jquery-ui-widget-factory-bridge/' rel='bookmark' title='Using &lt;code&gt;$.widget.bridge&lt;/code&gt; Outside of the Widget Factory'>Using <code>$.widget.bridge</code> Outside of the Widget Factory</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/tips-for-developing-jquery-ui-widgets/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>A New And Improved jQuery Idle Timeout Plugin</title>
		<link>http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/</link>
		<comments>http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 12:42:50 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[idle]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=492</guid>
		<description><![CDATA[This is a followup to an older post of mine about notifying &#38; prompting your users for action once they&#8217;ve become idle. I&#8217;ve since re-written and abstracted the logic, creating a much more useful &#38; customizable plugin. The main issue with my first implementation, which I&#8217;ve addressed in this update, is that the server was [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/creating-a-mint-com-style-idle-logout-timer-using-jquery/' rel='bookmark' title='Creating a Mint.com Style Idle Logout Timer Using jQuery'>Creating a Mint.com Style Idle Logout Timer Using jQuery</a></li>
<li><a href='http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/' rel='bookmark' title='A Recursive setTimeout Pattern'>A Recursive setTimeout Pattern</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-disabling-the-submit-button-when-a-form-is-submitted/' rel='bookmark' title='jQuery: modifying the submit button when a form is submitted'>jQuery: modifying the submit button when a form is submitted</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fa-new-and-improved-jquery-idle-timeout-plugin%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fa-new-and-improved-jquery-idle-timeout-plugin%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This is a followup to <a href="http://www.erichynds.com/jquery/creating-a-mint-com-style-idle-logout-timer-using-jquery/">an older post of mine</a> about notifying &amp; prompting your users for action once they&#8217;ve become idle.  I&#8217;ve since re-written and abstracted the logic, creating a much more useful &amp; customizable plugin.  The main issue with my first implementation, which I&#8217;ve addressed in this update, is that the server was never actually polled to maintain an active server-side session during long periods of use.  The server-side session might be set to expire after 30 minutes, but a user could be active on your application without a page load for longer than that.  As such, following a page load after 30 minutes, the user would (presumably) be prompted to log back in&#8230; not a great user experience.</p>
<p><img src="/examples/jquery-idle-timeout/screenshot.gif" alt="jQuery Idle Timeout" /></p>
<p>The gist of here is that after a user is idle after a configurable period of time, a div/UI dialog/whatever opens, warning the user that his/her session is about to expire.  The user has X number of seconds to click a &#8220;resume&#8221; link in order to continue their session.  If this event is not fired, the user is relocated (configurable) to a page where you&#8217;d likely implement server-side code to end their session.  Idle state is one without any mouse movement or keypresses, and is detected with Paul Irish&#8217;s <a class="blank" href="http://paulirish.com/2009/jquery-idletimer-plugin/">idleTimer</a> plugin.<span id="more-492"></span></p>
<ul>
<li>See the <a href="http://www.erichynds.com/examples/jquery-idle-timeout/example-mint.htm">Mint.com-style demo</a>, or a demo using the <a href="http://www.erichynds.com/examples/jquery-idle-timeout/example-dialog.htm">jQuery UI dialog widget</a>.</li>
<li><a href="http://github.com/ehynds/jquery-idle-timeout">Download the source &amp; follow on GitHub</a>.</li>
</ul>
<h2>Polling requests keep the server-side session alive</h2>
<p>Polling requests are automatically sent to the server at a configurable interval, maintaining the users session while s/he is using your application for long periods of time.  My personal use case for this was an application where the user could write and send emails, but some users took so long to write their message that the server-side session expired before they submitted the form and their e-mail was lost.  With ColdFusion and presumably other scripting languages, hitting a CFM file is enough to reset the internal server-side session timer.</p>
<p>Polling is done with a simple <code>setInterval</code> call so requests are not queued per se, but you are protected against failed &#038; built-up requests.  If the AJAX request exceeds your configured timeout limit, the request fails, OR the response does not match explicitly what you expect X amount of times, the script aborts itself.  This was designed to prevent long-running requests from building up and unnecessarily polling the server when the server isn&#8217;t even responding properly.  The likely scenario here is if the server goes down while the user is working on your page, or is overloaded and sporadically throwing 500&#8242;s or similar.</p>
<h2>Usage</h2>
<p>Here&#8217;s a quick mockup of a demo that opens a UI dialog window after the user is idle for 5 minutes.  The user can choose to either keep his/her session alive, or end the session.  A live demo of this can be found <a href="http://www.erichynds.com/examples/jquery-idle-timeout/example-dialog.htm">here</a>.  First, markup the dialog&#8217;s HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- dialog window markup --&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;dialog&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your session is about to expire!&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>You will be logged off in <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;dialog-countdown&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span> seconds.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Do you want to continue your session?<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Next, initiate the UI dialog widget:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// setup the dialog</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	autoOpen<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	modal<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	width<span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>
	height<span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span>
	closeOnEscape<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	draggable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	resizable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
	buttons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #3366CC;">'Yes, Keep Working'</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: #006600; font-style: italic;">// Just close the dialog. We pass a reference to this</span>
			<span style="color: #006600; font-style: italic;">// button during the init of the script, so it'll automatically</span>
			<span style="color: #006600; font-style: italic;">// resume once clicked</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'close'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #3366CC;">'No, Logoff'</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: #006600; font-style: italic;">// fire whatever the configured onTimeout callback is.</span>
			$.<span style="color: #660066;">idleTimeout</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">onTimeout</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Finally, init the idleTimeout plugin.  We&#8217;ll pass in the #dialog div as the first parameter, and the button that will trigger the &#8220;resume&#8221; functionality as the second parameter.  In this demo, the resume logic needs to be bound to the &#8220;Yes&#8230;&#8221; button, which can be found by retrieving the <code>:first</code> button in the div <code>.ui-dialog-buttonpane</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// start the plugin</span>
$.<span style="color: #660066;">idleTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#dialog'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'div.ui-dialog-buttonpane button:first'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
	idleAfter<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// user is considered idle after 5 minutes of no movement</span>
	pollingInterval<span style="color: #339933;">:</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// a request to keepalive.php (below) will be sent to the server every minute</span>
	keepAliveURL<span style="color: #339933;">:</span> <span style="color: #3366CC;">'keepalive.php'</span><span style="color: #339933;">,</span>
	serverResponseEquals<span style="color: #339933;">:</span> <span style="color: #3366CC;">'OK'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the response from keepalive.php must equal the text &quot;OK&quot;</span>
	onTimeout<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// redirect the user when they timeout.</span>
		window.<span style="color: #660066;">location</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;timeout.htm&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	onIdle<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// show the dialog when the user idles</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	onCountdown<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>counter<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// update the counter span inside the dialog during each second of the countdown</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog-countdown&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	onResume<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// the dialog is closed by a button in the dialog</span>
		<span style="color: #006600; font-style: italic;">// no need to do anything else</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Options</h2>
<table class="parameters" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width: 125px;">Parameter</th>
<th>Description</th>
<th style="width: 100px;">Default</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">warningLength</td>
<td>The number of seconds after user is idle to show the logout warning.</td>
<td>30</td>
</tr>
<tr>
<td class="parameter">keepAliveURL</td>
<td>A url to call to keep the session alive while the user is active.</td>
<td>30</td>
</tr>
<tr>
<td class="parameter">serverResponseEquals</td>
<td>The response from keepAliveURL must equal this text.</td>
<td>OK</td>
</tr>
<tr>
<td class="parameter">idleAfter</td>
<td>The user is considered idle after this many seconds.  10 minutes default.</td>
<td>600</td>
</tr>
<tr>
<td class="parameter">pollingInterval</td>
<td>A polling request will be sent to the server every X seconds.</td>
<td>30</td>
</tr>
<tr>
<td class="parameter">failedRequests</td>
<td>The number of failed polling requests until the script is aborted.</td>
<td>5</td>
</tr>
<tr>
<td class="parameter">AJAXTimeout</td>
<td>The timeout passed to $.ajax in milliseconds.</td>
<td>250</td>
</tr>
<tr>
<td class="parameter">onResume</td>
<td>A callback to fire when the session is resumed (by clicking the resume link).</td>
<td>function(){}</td>
</tr>
<tr>
<td class="parameter">onIdle</td>
<td>Fires when the user becomes idle.</td>
<td>function(){}</td>
</tr>
<tr>
<td class="parameter">onCountdown</td>
<td>Fires during each second of the warningLength parameter.</td>
<td>function(){}</td>
</tr>
<tr>
<td class="parameter">onTimeout</td>
<td>A callback to fire when the session times out.</td>
<td>function(){}</td>
</tr>
<tr>
<td class="parameter">onAbort</td>
<td>A callback to fire when the script is aborted due to too many failed polling requests.</td>
<td>function(){}</td>
</tr>
</tbody>
</table>
<p>Enjoy!</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/creating-a-mint-com-style-idle-logout-timer-using-jquery/' rel='bookmark' title='Creating a Mint.com Style Idle Logout Timer Using jQuery'>Creating a Mint.com Style Idle Logout Timer Using jQuery</a></li>
<li><a href='http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/' rel='bookmark' title='A Recursive setTimeout Pattern'>A Recursive setTimeout Pattern</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-disabling-the-submit-button-when-a-form-is-submitted/' rel='bookmark' title='jQuery: modifying the submit button when a form is submitted'>jQuery: modifying the submit button when a form is submitted</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/feed/</wfw:commentRss>
		<slash:comments>64</slash:comments>
		</item>
		<item>
		<title>Visualizing Your Z-Index Stacks with jQuery</title>
		<link>http://www.erichynds.com/jquery/visualizing-your-z-index-stacks-with-jquery/</link>
		<comments>http://www.erichynds.com/jquery/visualizing-your-z-index-stacks-with-jquery/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 12:59:37 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[bookmarklet]]></category>
		<category><![CDATA[favlet]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=379</guid>
		<description><![CDATA[If you&#8217;ve ever found yourself debugging multiple elements stacked with z-index, and wish you could visualize their boundaries and stack positions without trying to remember numbers, this one is for you. The picture on the right pretty much sums it up. &#8220;Visualize Stack&#8221; as I call it applies different shades of a color to elements [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fvisualizing-your-z-index-stacks-with-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fvisualizing-your-z-index-stacks-with-jquery%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><img src="/examples/jquery-visualize-stack/img/example.png" alt="Example" style="float:right;padding:0 0 8px 8px" />If you&#8217;ve ever found yourself debugging multiple elements stacked with z-index, and wish you could visualize their boundaries and stack positions without trying to remember numbers, this one is for you.  </p>
<p>The picture on the right pretty much sums it up.  &#8220;Visualize Stack&#8221; as I call it applies different shades of a color to elements based on their z-index value, relative to the z-index of other elements.  This is particularly useful for developers who are stacking multiple elements on top of each other and would like to visually see their layering.  <b>For the time being, this plugin only works on browsers that support CSS3 hsl() coloring.  Sorry, IE.</b></p>
<ul style="margin-bottom:12px">
<li><a href="/examples/jquery-visualize-stack/">View an example</a></li>
<li><a class="blank" href="http://github.com/ehynds/jquery-visualize-stack">Get the code</a> and follow this project on GitHub</li>
<li><a href="#bookmarklet">Get the bookmarklet</a></li>
</ul>
<h2>Usage</h2>
<p>Call the visualizeStack() method on a jQuery object.  The plugin will internally filter and only apply logic to the elements with a positive or negative numerical z-index.  Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// the logic will only apply to div and p elements in this example.  </span>
	<span style="color: #006600; font-style: italic;">// use $(&quot;*&quot;) for all elements.</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div, p&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">visualizeStack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// CSS rules to apply.  the percent sign (%) is replaced with </span>
		<span style="color: #006600; font-style: italic;">// the appropriate color shade.</span>
		properties<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			background<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;%&quot;</span><span style="color: #339933;">,</span>
			border<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1px solid black&quot;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// the base color of the gradients.  Enter one of the following</span>
		<span style="color: #006600; font-style: italic;">// colors, or a hue number for hsl(). accepted color names: </span>
		<span style="color: #006600; font-style: italic;">// red, purple, blue, turquoise, green, orange, and yellow.</span>
		color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">,</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// a hex color to apply to very lightly/darkly shaded elements.  </span>
		<span style="color: #006600; font-style: italic;">// the &quot;color&quot; property of these</span>
		<span style="color: #006600; font-style: italic;">// elements will be set to this value.  pass an empty string </span>
		<span style="color: #006600; font-style: italic;">// to disable.</span>
		lightTextColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#fff&quot;</span><span style="color: #339933;">,</span>
		darkTextColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#000&quot;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a name="bookmarklet"></a></p>
<h2>The Bookmarket</h2>
<p>If you&#8217;re like me, you&#8217;re probably too lazy to come to this website, download the JS file, include it, and write the selector/plugin call just to debug some simple CSS stack issues.  By the time you&#8217;ve done all that it&#8217;s likely you could have figured the problem out on your own.  Therefore, I packaged this code up in a little bookmarklet so you can instantly apply plugin.</p>
<p>A couple caveats first:  1) you must already have jQuery included in the website, and 2) it isn&#8217;t configurable unless you change the bookmarklet code yourself.  All elements are selected (using $(&#8220;*&#8221;)) and the default options are automatically applied (the background property changes to shades of red).</p>
<p>To use, drag this link into your bookmarks toolbar: <a href="javascript:(function($){$.fn.visualizeStack=function(options){options=$.extend({},$.fn.visualizeStack.defaults,options);var%20elems=this.filter(function(){return%20$(this).css('z-index')%20!==%20'auto';}).map(function(i,elem){return%20{element:elem,stack:$(this).css('z-index')};}).get().sort(function(a,b){return%20a.stack%20===%20b.stack%20?%200%20:%20(a.stack<b.stack%20?%20-1%20:%201);});if(!this.length%20||%20!elems.length){return%20this;}var%20lastColor=90,lastStack=elems[0]['stack'],startHue=/\w/.test(options.color)%20?%20$.fn.visualizeStack.colors[options.color]%20:%20options.color;var%20uniqueStacks=(function(){var%20stacks=$.map(elems,function(obj){return%20obj.stack;}),uniques=[];$.each(stacks,function(i,val){if($.inArray(val,uniques)%20===%20-1){uniques.push(val);}});return%20uniques.length;})();$.each(elems,function(i,obj){var%20$element=$(obj.element),color=lastColor=(obj.stack%20===%20lastStack)?lastColor:lastColor-(100/uniqueStacks);$.each(options.properties,function(prop,val){$element.css(prop,val.replace('%','hsl('+startHue+',100%,'+color+'%)'));if(options.invertTextColor.length%20&#038;&#038;%20lastColor<=50){$element.css('color',options.invertTextColor);}});lastStack=obj.stack;});return%20this;};$.fn.visualizeStack.colors={red:360,blue:240,turquoise:180,green:120,yellow:60};$.fn.visualizeStack.defaults={color:'red',properties:{'background':'%'},invertTextColor:''};$('*').visualizeStack();})(jQuery);">Visualize Stack</a></p>
<p>I suggest giving this thing a run on <a href="http://www.digg.com" class="blank">Digg.com</a>; it&#8217;s pretty interesting to see what they have going over there for z-index&#8217;d elements.</p>
<p><span id="more-379"></span></p>
<h2>About hsl() Coloring</h2>
<p>This plugin dynamically generates a shade of a color with <a href="http://www.w3.org/TR/css3-color/" class="_blank">CSS3&#8242;s hsl(h,s,l)</a>, or hue, saturation, and lightness syntax.  One of the many advantages of hsl() over rgb() for this type of application is that you can begin with a hue color &#8211; say 90 for red &#8211; and essentially guess a different shade of that color by tweaking the lightness.  As such, providing an API where you can either pass in a number of a starting hue, or the name of a color, was trivial.  The color name and associated hue number map looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">visualizeStack</span>.<span style="color: #660066;">colors</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	red<span style="color: #339933;">:</span> <span style="color: #CC0000;">360</span><span style="color: #339933;">,</span>
	purple<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	blue<span style="color: #339933;">:</span> <span style="color: #CC0000;">240</span><span style="color: #339933;">,</span>
	turquoise<span style="color: #339933;">:</span> <span style="color: #CC0000;">180</span><span style="color: #339933;">,</span>
	green<span style="color: #339933;">:</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span>
	orange<span style="color: #339933;">:</span> <span style="color: #CC0000;">35</span><span style="color: #339933;">,</span>
	yellow<span style="color: #339933;">:</span> <span style="color: #CC0000;">60</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Unfortunately, support for hsl() is limited to Firefox 3, Google Chrome, Opera 9+, and Safari 3+.  IE doesn&#8217;t get to play yet, but I believe we&#8217;ll be seeing it in IE 9.  Also,  if you&#8217;re into hsl(), check out hsla() on the <a href="http://www.w3.org/TR/css3-color/" class="_blank">CSS3 color spec</a> which adds support for alpha transparency as well.</p>
<h2>Walking Through The Code</h2>
<p>The code for this is not terribly complicated.  First, we&#8217;ll create a plugin called &#8220;visualizeStack&#8221; and add it the the $.fn namespace so that it can be called on a jQuery object of elements.  We&#8217;ll allow an options parameter to be passed in, and then override the default options with the passed in options.  This defaults object will be defined later towards the end of the file.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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>
&nbsp;
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">visualizeStack</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">visualizeStack</span>.<span style="color: #660066;">defaults</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The next section involves filtering out only elements that have a numerical z-index, and saving a reference to each element it&#8217;s z-index value in an sorted array.  I&#8217;ve commented heavily here, so hopefully it explains itself:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// We only want to keep elements that have a legitimate z-index.</span>
<span style="color: #006600; font-style: italic;">// If this plugin is called like $(&quot;div&quot;).visualizeStack(), for </span>
<span style="color: #006600; font-style: italic;">// example, not all divs in that selection could have a set z-index.</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">// Because the default z-index property value is &quot;auto&quot; we can</span>
<span style="color: #006600; font-style: italic;">// safely assume a z-index was set if the value is numeric</span>
<span style="color: #003366; font-weight: bold;">var</span> elems <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">filter</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>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009966; font-style: italic;">/^-?\d+$/</span>.<span style="color: #660066;">test</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>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;z-index&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Take those filtered elements and return an array of objects.  The </span>
<span style="color: #006600; font-style: italic;">// &quot;element&quot; key will be a DOM element, and the &quot;stack&quot; key </span>
<span style="color: #006600; font-style: italic;">// will be the z-index.  </span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">// Ultimately we want to create an array sorted in order of </span>
<span style="color: #006600; font-style: italic;">// z-indexes, from lowest to hightest. But, we also need </span>
<span style="color: #006600; font-style: italic;">// to remember which element had which z-index; an array of just </span>
<span style="color: #006600; font-style: italic;">// z-indexes or just elements does not help us. </span>
.<span style="color: #660066;">map</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>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span> element<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> stack<span style="color: #339933;">:</span>parseFloat<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>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;z-index&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Turn this mapped jQuery object into a pure-javascript array of</span>
<span style="color: #006600; font-style: italic;">// object literals.  If we didn't call get(), we would still be working</span>
<span style="color: #006600; font-style: italic;">// with a jQuery object of object literals.</span>
.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Finally, sort the array ascending based on the &quot;stack&quot; property</span>
<span style="color: #006600; font-style: italic;">// of the object.  Returning hierarchal numbers in sort() determines where</span>
<span style="color: #006600; font-style: italic;">// to place the array item.</span>
.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> a.<span style="color: #660066;">stack</span> <span style="color: #339933;">===</span> b.<span style="color: #660066;">stack</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">stack</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> b.<span style="color: #660066;">stack</span> <span style="color: #339933;">?</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>At this point we&#8217;re going to bail out of the script if there aren&#8217;t any elements to work with, which could happen one of two ways:  1) either the call to filter() returned an empty set because there weren&#8217;t any elements with a numerical z-index, or 2) when this plugin was created using $(expr).visualizeStack() syntax, the $(expr) call did not return any elements.</p>
<p>It is important to return &#8220;this&#8221; (the jQuery object of matching DOM elements) here to preserve chainability.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// nothing found?  bail.</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>elems.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Set a few variables we&#8217;ll use later for calculating the lightness hsl() value:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> 
&nbsp;
<span style="color: #006600; font-style: italic;">// the last &quot;lightness&quot; number will be recorded here.  We'll start with 90.</span>
lastColor <span style="color: #339933;">=</span> <span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> 
&nbsp;
<span style="color: #006600; font-style: italic;">// the last z-index level will be recorded here.  Start with the first</span>
<span style="color: #006600; font-style: italic;">// z-index in our elems array, or the lowest z-index.</span>
lastStack <span style="color: #339933;">=</span> elems<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'stack'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// To define the gradient spectrum color, users can either pass in a</span>
<span style="color: #006600; font-style: italic;">// name of a color (like red) OR a starting hue color.  If the user</span>
<span style="color: #006600; font-style: italic;">// passed in a name, lookup it's hue value in $.fn.visualStack.colors.</span>
<span style="color: #006600; font-style: italic;">// otherwise, use the number.  I'm making an assumption users will read</span>
<span style="color: #006600; font-style: italic;">// the docs and don't try to enter the name of an unsupported color.</span>
startHue <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/\w/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">color</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">visualizeStack</span>.<span style="color: #660066;">colors</span><span style="color: #009900;">&#91;</span>options.<span style="color: #660066;">color</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> options.<span style="color: #660066;">color</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, we need to figure out how many unique z-indexes exist so we can correctly calculate a shade of color (or lightness), which is later determined by dividing the number of unique z-indexes by 100, then subtracting the quotient by the last color hue number. We cannot rely on the total number of elements because 8 out of 10 could have the same z-index level.  The shades of colors would not be applied evenly across the spectrum.</p>
<p>To see what I&#8217;m talking about, this is an example of the shades of red calculated when we use the total number of elements:</p>
<div class="center"><img src="/examples/jquery-visualize-stack/img/numstacks_total.png" alt="" /></div>
<p>If we use the unique number of z-indexes, the shades of red cover a wider spectrum:</p>
<div class="center"><img src="/examples/jquery-visualize-stack/img/numstacks_unique.png" alt="" /></div>
<p>To calculate this number we&#8217;ll use a self-executing anonymous function to loop through the <code>elems</code> array, and add each element&#8217;s z-index to a new array called <code>uniques</code> unless it has already been added.  The length of <code>uniques</code> is then returned and set to the variable <code>uniqueStacks</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// figure out how many unique z-index there are</span>
<span style="color: #003366; font-weight: bold;">var</span> uniqueStacks <span style="color: #339933;">=</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>
&nbsp;
	<span style="color: #006600; font-style: italic;">// where we'll store the unique z-indexes</span>
	<span style="color: #003366; font-weight: bold;">var</span> uniques <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>elems<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">inArray</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">stack</span><span style="color: #339933;">,</span> uniques<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			uniques.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">stack</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> uniques.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next comes the heart of the plugin.  With each element, we need to determine the lightness value to use based its z-index, and apply the CSS properties defined in the option&#8217;s &#8220;properties&#8221; key.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// loop through the array of objects</span>
$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>elems<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// jQuerify our element</span>
	<span style="color: #003366; font-weight: bold;">var</span> $element <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// determine the lightness value.  if this z-index</span>
		<span style="color: #006600; font-style: italic;">// is the same as the last z-index, use the last lightness</span>
		<span style="color: #006600; font-style: italic;">// value.  otherwise, create a new, darker shade</span>
		color <span style="color: #339933;">=</span> lastColor <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">stack</span> <span style="color: #339933;">===</span> lastStack<span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">?</span> lastColor
			<span style="color: #339933;">:</span> lastColor<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">/</span>uniqueStacks<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// loop through the CSS properties to add</span>
	$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">properties</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>prop<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// replace the percent sign in the property value with the</span>
		<span style="color: #006600; font-style: italic;">// hsl() color.  &quot;background-color: %&quot; will become</span>
		<span style="color: #006600; font-style: italic;">// &quot;background-color: hsl(h, s, l)&quot;</span>
		$element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span>prop<span style="color: #339933;">,</span> val.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;%&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;hsl(&quot;</span><span style="color: #339933;">+</span>startHue<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;,100%,&quot;</span><span style="color: #339933;">+</span>color<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;%)&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// change the text color property when the shade becomes </span>
		<span style="color: #006600; font-style: italic;">// too light or too dark to read</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">lightTextColor</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;&amp;</span> lastColor <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span> options.<span style="color: #660066;">lightTextColor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">darkTextColor</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;&amp;</span> lastColor <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span> options.<span style="color: #660066;">darkTextColor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// remember what the last z-index value was</span>
	lastStack <span style="color: #339933;">=</span> obj.<span style="color: #660066;">stack</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We&#8217;re almost done!  The final steps are to return &#8220;this&#8221; so that this plugin can be chained, create a lookup table of color hue&#8217;s, define this plugin&#8217;s default options, and close off the wrapping closure.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// close $.fn.visualizeStack</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// remember how you can pass in a color name as the &quot;color&quot;</span>
<span style="color: #006600; font-style: italic;">// property?  these are the supported colors and their hue</span>
<span style="color: #006600; font-style: italic;">// equivalents for CSS3's hsl()</span>
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">visualizeStack</span>.<span style="color: #660066;">colors</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	red<span style="color: #339933;">:</span> <span style="color: #CC0000;">360</span><span style="color: #339933;">,</span>
	purple<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span>
	blue<span style="color: #339933;">:</span> <span style="color: #CC0000;">240</span><span style="color: #339933;">,</span>
	turquoise<span style="color: #339933;">:</span> <span style="color: #CC0000;">180</span><span style="color: #339933;">,</span>
	green<span style="color: #339933;">:</span> <span style="color: #CC0000;">120</span><span style="color: #339933;">,</span>
	orange<span style="color: #339933;">:</span> <span style="color: #CC0000;">35</span><span style="color: #339933;">,</span>
	yellow<span style="color: #339933;">:</span> <span style="color: #CC0000;">60</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// plugin defaults</span>
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">visualizeStack</span>.<span style="color: #660066;">defaults</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">,</span>
	properties<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #3366CC;">&quot;background&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;%&quot;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
	lightTextColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// close the wrapping closure, passing in the jQuery object</span>
<span style="color: #006600; font-style: italic;">// so $ can be used inside this plugin without conflict</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s all there is to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/visualizing-your-z-index-stacks-with-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery MultiSelect Plugin w/ ThemeRoller Support</title>
		<link>http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/</link>
		<comments>http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 14:47:03 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[multiselect]]></category>
		<category><![CDATA[themeroller]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=240</guid>
		<description><![CDATA[07-09-2010 &#8211; This plugin has been re-written! I&#8217;ve re-written this plugin using the jQuery UI widget factory. As such, a number of new features have been added, bugs fixed, and performance issues addressed. Use the widget version instead &#8211; I won&#8217;t be doing any further development on this version. I&#8217;ve been working on a multiple [...]


Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/' rel='bookmark' title='jQuery UI MultiSelect Widget'>jQuery UI MultiSelect Widget</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/' rel='bookmark' title='jQuery Related (Dependent) Selects Plugin'>jQuery Related (Dependent) Selects Plugin</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fjquery-multiselect-plugin-with-themeroller-support%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fjquery%2Fjquery-multiselect-plugin-with-themeroller-support%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<div class="update" style="margin-top:15px;padding:10px;width:525px">
<div class="bold" style="font-size:16px">07-09-2010 &#8211; This plugin has been re-written!</div>
<p>I&#8217;ve <a href="/jquery/jquery-ui-multiselect" class="bold">re-written</a> this plugin using the jQuery UI widget factory.  As such, a number of new features have been added, bugs fixed, and performance issues addressed.  </p>
<p><a class="bold" href="/jquery/jquery-ui-multiselect">Use the widget version instead</a> &#8211; I won&#8217;t be doing any further development on this version.
</div>
<p>I&#8217;ve been working on a multiple select plugin on and off for the past couple months and finally have it stable enough for a first release.  When I started this project my intentions were only to re-factor <a href="http://abeautifulsite.net/2008/04/jquery-multiselect/">Cory LaViska&#8217;s MultiSelect</a> implementation, but it quickly turned into a complete re-write with a focus on speed and ThemeRoller support.  This plugin turns an ordinary HTML select control into an elegant drop down list of checkboxes.<br />
<img style="float: right; padding: 5px 0 10px 10px;" src="/examples/jquery-multiselect/screenshot.gif" alt="jQuery MultiSelect" /></p>
<ul>
<li>Current version: 0.6 (05/04/2010 &#8211; <a href="http://github.com/ehynds/jquery-multiselect/raw/0.6/CHANGELOG" class="blank">Changelog</a>)</li>
<li><a class="blank" href="/examples/jquery-multiselect/examples.htm">View demos</a></li>
<li>Download <a class="blank" href="http://github.com/ehynds/jquery-multiselect/raw/0.6/src/jquery.multiselect.js">source (12.5 kb)</a> or <a class="blank" href="http://github.com/ehynds/jquery-multiselect/raw/0.6/src/jquery.multiselect.min.js">minified (6.8 kb)</a>, and the <a href="http://github.com/ehynds/jquery-multiselect/raw/0.6/jquery.multiselect.css" class="blank">CSS file</a>.</li>
<li>Follow this project on <a class="blank" href="http://github.com/ehynds/jquery-multiselect">GitHub</a></li>
</ul>
<h2>Features</h2>
<ul>
<li>Full ThemeRoller support.</li>
<li>Optgroup support with clickable labels.</li>
<li>Optional header with check all / uncheck all / close links.</li>
<li>Degrades gracefully.</li>
<li>Keyboard support.</li>
<li>Ability to hook into 5 different event callbacks.</li>
<li>Display the checked options in a list with a configurable maximum.</li>
<li>Easily change the position, fade speed, scroll container height, links text, and input text.</li>
<li>The widget width inherits from the original element, but is also configurable as a parameter.</li>
<li>Pre-selected &#038; disabled option/widget support.</li>
<li>bgiframe support.</li>
<li>Only 6.9kb minified (2kb Gzipped).</li>
</ul>
<p><span id="more-240"></span></p>
<h2>Compatibility</h2>
<p>MultiSelect is compatible with jQuery 1.4.0+ and all themes from jQuery UI 1.7+.  This plugin is known to work in (but not limited to):</p>
<ul>
<li>Firefox 2 &#8211; 3.6</li>
<li>IE 6 &#8211; 8</li>
<li>Chrome Beta/4</li>
<li>Safari 4</li>
<li>Opera 10</li>
</ul>
<h2>Usage</h2>
<p>First ensure you&#8217;ve included jQuery 1.4, a jQuery UI theme of your choice, and the jquery.multiselect.css file.  You don&#8217;t need the jQuery UI library itself, just the theme files.  The simplest use of MultiSelect is to bind the widget to your select box:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MySelectBox&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multiple&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;MySelectBox&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>Option 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>Option 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Do not forget to set your select boxes to <code>multiple="multiple"</code> if you want this plugin to degrade gracefully.  Any option tags with the <code>selected="selected"</code> attribute will automatically be checked by default, and any option tags with the <code>disabled="disabled"</code> attribute will automatically be disabled by default.</p>
<h3>Callbacks</h3>
<p>Inside the <code>onOpen</code> callback, <code>this</code> refers to the container holding the header and checkboxes.  If you want to gather all the checkboxes inside the multiselect that just opened, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	onOpen<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: #003366; font-weight: bold;">var</span> $checkboxes <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>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Inside the <code>onClick</code> callback, <code>this</code> refers to the checkbox that received the event.  Example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	onClick<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: #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;">checked</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was just checked!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>See the options below for a full list of callbacks.</p>
<h3>Overriding Options</h3>
<p>Options for this plugin are exposed in <code>$.fn.multiSelect.defaults</code>, so you can override the default options for all instances like such:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// set the minWidth parameter for all instances to 500 pixels</span>
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">multiSelect</span>.<span style="color: #660066;">defaults</span>.<span style="color: #660066;">minWidth</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Passing a function to the selectedText parameter</h3>
<p>As of 0.5, the selectedText parameter can accept an anonymous function with three arguments: the number of checkboxes checked, the total number of checkboxes, and an array of the checked checkbox DOM elements.  As you can see in the example, this gives you 100% control of the display:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#MySelectBox&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">multiSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	selectedText<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>numChecked<span style="color: #339933;">,</span> numTotal<span style="color: #339933;">,</span> checkedInputs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">// example: emulating the default behavior</span>
		<span style="color: #000066; font-weight: bold;">return</span> numChecked <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; checked&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
		<span style="color: #006600; font-style: italic;">// example: emulating the selectedList option</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>numChecked <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> 
			<span style="color: #339933;">?</span> checkedInputs.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> element.<span style="color: #660066;">title</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">', '</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">:</span> numChecked <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; checked&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Options</h2>
<p>This plugin is configurable with the following options:</p>
<table class="parameters" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th style="width:125px">Parameter</th>
<th>Description</th>
<th style="width:115px">Default</th>
</tr>
</thead>
<tbody>
<tr>
<td class="parameter">showHeader</td>
<td class="">A boolean value denoting whether or not to display the header containing the check all, uncheck all, and close links.</td>
<td>true</td>
</tr>
<tr>
<td class="parameter">maxHeight</td>
<td class="">The maximum height in pixels of the scrolling container that holds the checkboxes.</td>
<td>175</td>
</tr>
<tr>
<td class="parameter">minWidth</td>
<td class="">The minimum width in pixels of widget.  Setting to <code>auto</code> (default) will inherit the width from the original select element.</td>
<td>200</td>
</tr>
<tr>
<td class="parameter">checkAllText</td>
<td class="">The default text of the &#8220;Check all&#8221; link.</td>
<td>Check all</td>
</tr>
<tr>
<td class="parameter">unCheckAllText</td>
<td class="">The default text of the &#8220;Uncheck all&#8221; link.</td>
<td>Uncheck all</td>
</tr>
<tr>
<td class="parameter">noneSelectedText</td>
<td class="">
<div class="update">Renamed from <code>noneSelected</code> in 0.5!</div>
<p> The default text the select box when no options have been selected.</td>
<td>Select options</td>
</tr>
<tr>
<td class="parameter">selectedList</td>
<td class="">A boolean/numeric value denoting whether or not to display the checked opens in a list, and how many.  A number greater than 0 denotes the maximum number of list items to display before switching over to the <code>selectedText</code> parameter.  A value of 0 or <code>false</code> is disabled.</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">selectedText</td>
<td class="">
<p>The text to display in the select box when options are selected (if selectedList is false).  The pound sign (#) is automatically replaced by the number of checkboxes selected.  If two pound signs are present in this parameter, the second will be replaced by the total number of checkboxes available.  Example: &#8220;# of # checked&#8221;.</p>
<p>
<div class="update">New in 0.5!</div>
<p>As of 0.5, this parameter can also accept an anonymous function with three arguments: the number of checkboxes checked, the total number of checkboxes, and an array of the checked checkbox DOM elements.  See the examples.</p>
</td>
<td># selected</td>
</tr>
<tr>
<td class="parameter">position</td>
<td class="">The position of the options menu relative to the input control: top, middle, or bottom.</td>
<td>bottom</td>
</tr>
<tr>
<td class="parameter">shadow</td>
<td class="">A boolean value denoting whether to apply a CSS shadow (class ui-multiselect-shadow).</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">fadeSpeed</td>
<td class="">How fast (in ms) to fade out the options menu on close.</td>
<td>200</td>
</tr>
<tr>
<td class="parameter">state</td>
<td class="">
<div class="update">New in 0.5!</div>
<p> The default state of the widget.  Either open or closed.</td>
<td>closed</td>
</tr>
<tr>
<td class="parameter">disabled</td>
<td class="">
<div class="update">New in 0.5!</div>
<p> Whether or not to disabled the widget by default.  <b>Important:</b> see the &#8220;Known Issues&#8221; section below for more documentation on this.</td>
<td>false</td>
</tr>
<tr>
<td class="parameter">onCheck</td>
<td class="">A callback to fire when a checkbox is checked.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onOpen</td>
<td class="">A callback to fire when the options menu is opened.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onCheckAll</td>
<td class="">A callback to fire when the &#8220;Check all&#8221; link is clicked.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onUncheckAll</td>
<td class="">A callback to fire when the &#8220;Uncheck all&#8221; link is clicked.</td>
<td>Function</td>
</tr>
<tr>
<td class="parameter">onOptgroupToggle</td>
<td class="">A callback to fire when the opgroup header is clicked.  An array of checkboxes inside the optgroup is passed as an optional argument.</td>
<td>Function</td>
</tr>
</tbody>
</table>
<p></p>
<p><a name="issues"></a></p>
<h2>Known Issues</h2>
<p>A few issues are still outstanding:</p>
<ul>
<li>No onClose callback yet.</li>
<li>The position should automatically change if the current position will prevent the options menu from showing.  E.g., if the input is at the bottom of the page, the options should appear not appear below the input.</li>
<li>In IE, the shadow parameter increases the width and height of the container, rendering it a few pixels wider than the input and throwing off the alignment.  You can easily edit the CSS file and remove the IE-specific shadow filters, however, or opt not to use it.</li>
<li>If the select box you&#8217;re transforming into a multiselect is disabled, the option tags will actually inherit the disabled property only in webkit; Firefox and IE do not display this behavior.  I&#8217;ve <a href="http://dev.jquery.com/ticket/6211">filed a bug</a> on this.  Therefore, to be cross-browser compatible, if the entire widget is disabled by default, the class ui-state-disabled is applied and the disabled attribute is removed.  This is important to note because checkbox values could be transmitted during submit, even if the select is disabled.  To toggle disabled state, toggle the ui-state-disabled class on the widget.
<p>This bug does NOT effect the disabled state of specific option tags.</li>
</ul>
</pre>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/' rel='bookmark' title='jQuery UI MultiSelect Widget'>jQuery UI MultiSelect Widget</a></li>
<li><a href='http://www.erichynds.com/jquery/jquery-related-dependent-selects-plugin/' rel='bookmark' title='jQuery Related (Dependent) Selects Plugin'>jQuery Related (Dependent) Selects Plugin</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/jquery/jquery-multiselect-plugin-with-themeroller-support/feed/</wfw:commentRss>
		<slash:comments>259</slash:comments>
		</item>
	</channel>
</rss>

