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

<channel>
	<title>Learning jQuery &#187; DOM Traversing</title>
	<atom:link href="http://www.learningjquery.com/category/types/dom-traversing/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.com</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Tue, 17 Jan 2012 14:14:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using jQuery&#8217;s .pushStack() for reusable DOM traversing methods</title>
		<link>http://www.learningjquery.com/2011/12/using-jquerys-pushstack-for-reusable-dom-traversing-methods</link>
		<comments>http://www.learningjquery.com/2011/12/using-jquerys-pushstack-for-reusable-dom-traversing-methods#comments</comments>
		<pubDate>Tue, 20 Dec 2011 18:51:07 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Intermediate]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1324</guid>
		<description><![CDATA[The .pushStack() method has been in jQuery since before version 1.0, but it hasn't received a whole lot of attention outside of core developers and plugin authors. While its usefulness may not be immediately apparent, it can come in really handy in some situations, so I'd like to take a quick look at what it [...]]]></description>
			<content:encoded><![CDATA[<p>The <code>.pushStack()</code> method has been in jQuery since before version 1.0, but it hasn't received a whole lot of attention outside of core developers and plugin authors. While its usefulness may not be immediately apparent, it can come in really handy in some situations, so I'd like to take a quick look at what it does, how it works, and how we can use it.</p>

  <h4>pushStack Basics </h4>
  <p>At its most basic level, the <code>.pushStack()</code> method accepts an array of DOM elements and "pushes" it onto a "stack" so that later calls to methods like <code>.end()</code> and <code>.andSelf()</code> behave correctly. (Side note: As of jQuery 1.4.2, you can pass in a jQuery object instead of an array, but that isn't documented and jQuery itself always uses an array, so that's what we'll stick to here.)</p>
  <p>Internally, jQuery uses <code>.pushStack()</code> to keep track of the previous jQuery collections as you chain <a href="http://api.jquery.com/category/traversing/">traversing methods</a> such as <code>.parents()</code> and <code>.filter()</code>. This lets us traverse through the DOM, do some stuff, "back up" to previous collections within the same chain using <code>.end()</code>, and then do something else. Here is a somewhat contrived example:</p>
<span id="more-1324"></span>
<div class="igBar"><span id="ljavascript-8"><a href="#" onclick="javascript:showPlainTxt('javascript-8'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-8">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// select some divs</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.container'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// find some spans inside those divs and add a class to them</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'baby'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// pop those spans off the &quot;stack&quot;,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// returning to the previous collection (div.container)</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=end"><span style="">end</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// add a class to the parent of each div.container</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'daddy'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Because <code>.find()</code> returns the result of a <code>.pushStack()</code> call to keep track of the previous collection (as does <code>.parent()</code>), we can use <code>.end()</code> in the above example to return to the container divs.</p>

<h4>Using pushStack for Fun and Profit</h4>
<p>So, this is great for jQuery, but what can <code>.pushStack()</code> do for me and my code? Well, it can help me write specialized DOM traversal plugins that act just like jQuery's own traversal methods. In other words, I can stop chaining the same sets of traversal methods together and instead write a reusable function that still works with with <code>.end()</code> and all that. For example, let's say I often have a need to find an element's grandparent. While I could write <code>$('#myElement').parent().parent()</code> every time, it might be nice to just be able to write <code>$('#myElement').grandparent()</code> instead. A naïve way to write a grandparent plugin would look like this (changing the method name to "grandpa" for this example): </p>

<div class="igBar"><span id="ljavascript-9"><a href="#" onclick="javascript:showPlainTxt('javascript-9'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-9">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// NOT recommended!</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">grandpa</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parents"><span style="">parents</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parents"><span style="">parents</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The problem here is that <em>two</em> new jQuery object instances are added to the stack. So, let's see what happens when we use it:  </p>
<div class="igBar"><span id="ljavascript-10"><a href="#" onclick="javascript:showPlainTxt('javascript-10'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-10">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// The DOM looks like this:</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &lt;div class=&quot;grandpa&quot;&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &nbsp;&lt;div class=&quot;pa&quot;&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &nbsp; &nbsp;&lt;div class=&quot;child son&quot;&gt;&lt;/div&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &nbsp;&lt;/div&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &lt;/div&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.son'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">grandpa</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=end"><span style="">end</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.son'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span> elem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<div class="grandpa">
 <div class="pa">
   <div class="child son"></div>
 </div>
</div>

<p>Without seeing the plugin, we would expect to see <em>"child son"</em> inserted into <code>&lt;div class="son"&gt;</code>, but <em>"pa"</em> is inserted instead. Each <code>.parent()</code> call in the plugin adds to the stack, so using <code>.end()</code> only pops the second one off.</p>
<p>If we use <code>.pushStack()</code> instead, however, we can achieve the expected behavior:</p>

<div class="igBar"><span id="ljavascript-11"><a href="#" onclick="javascript:showPlainTxt('javascript-11'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-11">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">grandma</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> els <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=pushStack"><span style="">pushStack</span></a><span style="color: #009900;">&#40;</span> els.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Within a plugin function, one that is a method of<code> $.fn</code>, the <code>this</code> keyword refers to the jQuery object; therefore, the <code>els</code> variable refers to a jQuery object, as well. To convert it to an array, we use jQuery's <code>.get()</code> method, and we pass that array to <code>.pushStack()</code>. Let's see if <code>.grandma()</code> works any better than <code>.grandpa()</code>.</p>
<div class="igBar"><span id="ljavascript-12"><a href="#" onclick="javascript:showPlainTxt('javascript-12'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-12">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// The DOM looks like this:</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &lt;div class=&quot;grandma&quot;&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &nbsp;&lt;div class=&quot;ma&quot;&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &nbsp; &nbsp;&lt;div class=&quot;child daughter&quot;&gt;&lt;/div&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &nbsp;&lt;/div&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// &lt;/div&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> elem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.daughter'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">grandma</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=end"><span style="">end</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div.daughter'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span> elem.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<div class="grandma">
 <div class="ma">
   <div class="child daughter"></div>
 </div>
</div>

<p>Here, "child daughter" is inserted, which means that <code>.end()</code> works as expected, changing the jQuery collection from the result of <code>.grandma()</code> to the result of <code>$('div.daughter')</code>. So, we've just successfully written a DOM traversal plugin, albeit a very simple one.</p>

<h4>The Simplest DOM Traversal Methods </h4>
<p>If the plugin only uses one DOM traversal method, then <code>.pushStack()</code> isn't really necessary. The <a href="http://www.elijahmanor.com/2011/07/filterbydata-jquery-plugin.html">HTML5 data filter plugin</a> written by Elijah Manor illustrates this point nicely: </p>

<div class="igBar"><span id="ljavascript-13"><a href="#" onclick="javascript:showPlainTxt('javascript-13'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-13">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">filterByData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> type<span style="color: #339933;">,</span> value <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=filter"><span style="">filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> value <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">?</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> type <span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> value <span style="color: #339933;">:</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=data"><span style="">data</span></a><span style="color: #009900;">&#40;</span> type <span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Only one new jQuery collection is added to the stack, via <code>.filter()</code>, so using <code>.end()</code> simply pops that one off, and our job is done. </p>

<h4>Filtering grandparents</h4>
<p>For the sake of completeness, it would be nice for this DOM traversal plugin to allow optional "filtering" of the parent and grandparent elements. After all, jQuery's <code>.parent()</code>  and <code>.parents()</code> allow filtering. For example, if I were to write <code>$('div.child').parent('.daddy')</code>, the jQuery collection would only contain an element if <code>div.child</code> had a parent element and if that parent had a class of "daddy."</p>
<p>There are plenty of reasonable ways one could include the filters, but for my purposes I'm going to have a <code>.grandparent()</code> method <em>optionally</em> accept two arguments. If only one argument is provided, it will filter the <em>grandparent</em> element only; if two are provided, the first will filter the parent and the second will filter the grandparent. Here is the full plugin plugin: </p>

<div class="igBar"><span id="ljavascript-14"><a href="#" onclick="javascript:showPlainTxt('javascript-14'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-14">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">grandparent</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> parentFilter<span style="color: #339933;">,</span> grandFilter <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>grandFilter <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; grandFilter <span style="color: #339933;">=</span> parentFilter;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; parentFilter <span style="color: #339933;">=</span> undefined;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> els <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span> parentFilter <span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span> grandFilter <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=pushStack"><span style="">pushStack</span></a><span style="color: #009900;">&#40;</span> els.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Finally, we have a nice <code>.grandparent()</code> plugin that adheres to the contract set by other jQuery DOM traversal methods—one that works with both filters and the <code>.end()</code> method. Here is what it could look like in use.</p>

<iframe style="width: 100%; height: 400px" src="http://jsfiddle.net/kswedberg/JSDdb/embedded/js,html,result" allowfullscreen="allowfullscreen" frameborder="0"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2011/12/using-jquerys-pushstack-for-reusable-dom-traversing-methods/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Tab Navigation with Smooth Horizontal Sliding Using jQuery</title>
		<link>http://www.learningjquery.com/2009/09/tab-navigation-with-smooth-horizontal-sliding-using-jquery</link>
		<comments>http://www.learningjquery.com/2009/09/tab-navigation-with-smooth-horizontal-sliding-using-jquery#comments</comments>
		<pubDate>Fri, 18 Sep 2009 12:19:28 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[animations]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[JavaScript]]></category>

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

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

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

		<guid isPermaLink="false">http://www.learningjquery.com/?p=295</guid>
		<description><![CDATA[Easing can really bring life to an effect. Easing controls how an animation progresses over time by manipulating its acceleration. jQuery has two built-in easing methods: linear and swing. While they get the job done, they are pretty boring when compared to what's made available through the jQuery easing plugin. The jQuery easing plugin offers [...]]]></description>
			<content:encoded><![CDATA[<p>Easing can really bring life to an effect. Easing controls how an animation progresses over time by manipulating its acceleration. jQuery has two built-in easing methods: linear and swing. While they get the job done, they are pretty boring when compared to what's made available through the jQuery <a href="http://gsgd.co.uk/sandbox/jquery/easing/">easing plugin</a>. </p>

<p>The jQuery easing plugin offers 30 different easing methods, courtesy of <a href="http://www.robertpenner.com/easing/">Robert Penner's easing equations</a>. Let's check some of them out. </p>
<span id="more-295"></span>
<p>Easing can only be applied when using the <code>.animate()</code> method. The effects helper methods like <code>.show('fast')</code>, <code>.toggle('fast')</code>, <code>fadeIn('fast')</code>, and so on all just use the default easing, "swing."</p>

<p>There are two ways to use easing with the <code>.animate()</code> method. For example, let's say we want to animate the opacity of a particular div. The first way to call <code>.animate()</code> is like this:</p>

<div class="igBar"><span id="ljavascript-46"><a href="#" onclick="javascript:showPlainTxt('javascript-46'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-46">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// what we are animating</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">'fast'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// how fast we are animating</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">'swing'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the type of easing</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// the callback</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'done'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The second way is very similar and might even look the same at first glance. </p>

<div class="igBar"><span id="ljavascript-47"><a href="#" onclick="javascript:showPlainTxt('javascript-47'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-47">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myDiv'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// what we are animating</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'fast'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// how fast we are animating</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'swing'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the type of easing</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; complete<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 callback</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'done'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The difference is that the second method signature takes only two arguments: the properties we are animating and an object literal of settings/options. The first method signature separates out the settings/options into individual arguments. </p>

<h3>jQuery Easing Plugin</h3>

<p>Finally, let's look at a few of the 30 easing methods provided by the <a href="http://gsgd.co.uk/sandbox/jquery/easing/">easing plugin</a>. </p>

<h4>Ease Out Bounce</h4>
<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showPlainTxt('javascript-48'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-48">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#easing_example_1'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">200</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutBounce'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutBounce'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<div id="easing_example_1" class="easing_example">Click Me</div>
<p>&nbsp;</p>

<h4>Ease Out Elastic</h4>
<div class="igBar"><span id="ljavascript-49"><a href="#" onclick="javascript:showPlainTxt('javascript-49'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-49">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#easing_example_2'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">200</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutElastic'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutElastic'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<div id="easing_example_2" class="easing_example">Click Me</div>
<p>&nbsp;</p>

<h4>Ease Out Back</h4>
<div class="igBar"><span id="ljavascript-50"><a href="#" onclick="javascript:showPlainTxt('javascript-50'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-50">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#easing_example_3'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">200</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutBack'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duration<span style="color: #339933;">:</span> <span style="color: #3366CC;">'slow'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">'easeOutBack'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<div id="easing_example_3" class="easing_example">Click Me</div>
<p>&nbsp;</p>


<h3>jQuery UI Easing</h3>

<p>jQuery UI includes the easing plugin as part of its Effects Core. It also extends the effects helper methods to support the use of easing and even other types of effects. You can find the documentation for jQuery UI Effects <a href="http://docs.jquery.com/UI/Effects">here</a>.</p>
[inline][/inline]]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/02/quick-tip-add-easing-to-your-animations/feed</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>The Year of jQuery UI</title>
		<link>http://www.learningjquery.com/2009/01/the-year-of-jquery-ui</link>
		<comments>http://www.learningjquery.com/2009/01/the-year-of-jquery-ui#comments</comments>
		<pubDate>Sat, 03 Jan 2009 18:44:56 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[jQuery UI]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=223</guid>
		<description><![CDATA[Two years ago I made the somewhat immodest claim that 2007 would be the "Year of jQuery." Since then, jQuery's popularity has grown in ways that none of the core contributors could have imagined. Now I'm ready to make another bold pronouncement: 2009 will be the year of jQuery UI. Here's why: jQuery UI 1.6 [...]]]></description>
			<content:encoded><![CDATA[Two years ago I made the somewhat immodest claim that 2007 would be the "<a href="http://www.learningjquery.com/2007/01/the-year-of-jquery">Year of jQuery</a>." Since then, <a href="http://jquery.com">jQuery</a>'s <a href="http://www.google.com/trends?q=jquery">popularity</a> has grown in <a href="http://docs.jquery.com/Sites_Using_jQuery">ways</a> that none of the <a href="http://docs.jquery.com/Contributors">core contributors</a> could have imagined. Now I'm ready to make another bold pronouncement: 2009 will be the year of <a href="http://ui.jquery.com/">jQuery UI</a>. Here's why: <span id="more-223"></span>
<ul>
	<li><strong>jQuery UI 1.6 is shaping up to be a rock-solid re-factoring of the entire library.</strong> With <a href="http://blog.jquery.com/2008/12/31/jquery-ui-16rc3-its-getting-really-close/">RC4 released just two days ago</a>, the UI team have further cleaned up the API while providing a set of tools that are even more extensible and configurable than they were before.</li>
	<li><strong>Better project management and greater transparency</strong>. Over the last year or so, Paul Bakaus has done much more than lead the development of jQuery UI. He has shaped and nurtured a <a href="http://ui.jquery.com/about">team of top-notch JavaScript developers</a> and has opened up <a href="http://jqueryui.pbwiki.com/">a lot of the decision-making process</a> to outside scrutiny. This bodes well for the direction of the project.</li>
	<li><strong>Improved jQuery UI ThemeRoller</strong>. <a href="http://ui.jquery.com/ThemeRoller">ThemeRoller</a>, developed and designed by the folks at <a href="http://www.filamentgroup.com/">Filament Group</a>, was already an amazingly useful and elegant complement to jQuery UI. But now it's even better, with a much-improved CSS framework and lighter CSS footprint, a slick default theme, a cool icon sprite set, and more configuration options. Also, it'll soon be easier than ever to <a href="http://jqueryui.pbwiki.com/jQuery-UI-CSS-Framework">create your own UI widgets</a> that take advantage of the ThemeRoller &mdash; with upcoming tools, documentation, and tutorials for making your site "ThemeRoller-ready." </li>
	<li><strong>Improved documentation and demos</strong>. The <a href="http://docs.jquery.com/UI">documentation wiki</a> has undergone some significant updates in recent months, and the <a href="http://ui.jquery.com/demos">demos</a> went through a complete overhaul in coordination with the 1.6rc4 release.</li>
  <li><strong>More widespread adoption</strong>. As the API changes settle down and the core UI developers have more time to spend on providing more widgets and components, such as a spinner, menu, and colorpicker, more sites are sure to join the likes of WordPress in adopting jQuery UI.  </li>
	<li><strong>A new blog</strong>. While most of the details are still under wraps, a brand new weblog dedicated to learning jQuery UI, similar in quality and scope to <a href="http://www.learningjquery.com/">Learning jQuery</a>, will be online soon. </li>
</ul>
I have had the great pleasure to use jQuery UI on a couple recent projects, and I can honestly say that it has come a long way since its first release a year and a half ago. If you haven't used the UI library before, now is a great time to start. If my prediction comes true, you'll be at <a href="http://www.google.com/trends?q=jquery+ui&amp;ctab=0&amp;geo=all&amp;date=all&amp;sort=0">the forefront of a major trend</a>.

<h4><ins datetime="2009-01-03T21:33:43+00:00">Update:</ins></h4>
I failed to mention that the new blog will be run by jQuery UI developer <a href="http://rdworth.org/">Richard D. Worth</a>. In addition to being a great developer, Richard does an awesome job of promoting and explaining jQuery UI. I'm really looking forward to seeing the good stuff he'll be posting soon.]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/01/the-year-of-jquery-ui/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Peeling Away the jQuery Wrapper and Finding an Array</title>
		<link>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper</link>
		<comments>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper#comments</comments>
		<pubDate>Mon, 22 Dec 2008 16:46:56 +0000</pubDate>
		<dc:creator>Cody Lindley</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[objects]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=196</guid>
		<description><![CDATA[If you haven't poked around under the hood of jQuery, you might not be aware that when you pass the jQuery function an expression or DOM element it places these elements (or, possibly a single element) into an object, and then this object is returned so that it can be chained. Without getting into the [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven't poked around under the hood of jQuery, you might not be aware that when you pass the jQuery function an expression or DOM element it places these elements (or, possibly a single element) into an object, and then this object is returned so that it can be chained. Without getting into the details of chaining, the fundamental concept to remember is this:</p>

<p><em>Most jQuery methods return the jQuery object itself, which allows methods to be chained.</em></p>
<span id="more-196"></span>
<p>The interesting thing about the jQuery object is that while its datatype is an object, it has array-like characteristics:</p>
<ul>
	<li>its property names (the ones that refer to DOM elements, at least) are numeric</li>
	<li>it has a length property</li>
</ul>
<p>Have you ever noticed, for example, a snippet of jQuery code that directly accesses a DOM element by using the bracket operator with the jQuery function?</p>

<div class="igBar"><span id="ljavascript-58"><a href="#" onclick="javascript:showPlainTxt('javascript-58'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-58">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>; <span style="color: #006600; font-style: italic;">// give me the first DOM element that the</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// jQuery function found</span></div></li>
</ol></div>
</div></div><br />

<p>Now, the jQuery function returns an object, but it looks like we just treated it as an array. It can also be written like this:</p>

<div class="igBar"><span id="ljavascript-59"><a href="#" onclick="javascript:showPlainTxt('javascript-59'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-59">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// do the same, using a jQuery method</span></div></li>
</ol></div>
</div></div><br />

<p>If this is confusing, let's see if we can clear it up with some code. Go to the jQuery.com homepage, open up <a href="http://getfirebug.com/">Firebug</a> using Firefox, and run the following line of code in the console.</p>

<div class="igBar"><span id="ljavascript-60"><a href="#" onclick="javascript:showPlainTxt('javascript-60'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-60">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// toSource() is a method of all JavaScript objects</span></div></li>
</ol></div>
</div></div><br />

<p>You should be looking at this in the console:</p>

<code>"({length:25, 0:{}, 1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}, 7:{}, 8:{}, 9:{}, 10:{}, 11:{}, 12:{}, 13:{}, 14:{}, 15:{}, 16:{}, 17:{}, 18:{}, 19:{}, 20:{}, 21:{}, 22:{}, 23:{}, 24:{}, prevObject:{0:{}, length:1}})"</code>

<p>Looky there! The jQuery function is returning an object with a bunch of properties. If we tack on the bracket operator to the jQuery function, which returns an object, we can access the properties of that object because inside the object are properties with a numeric property name. Fascinating isn't it?</p>

<p>I know what you're thinking: this tutorial is about arrays, not objects. Right, so let's get to the array part. Since we now know that the jQuery function returns an object that only masquerades as a JavaScript array the question at hand is how do I get a true JavaScript array from a jQuery wrapper object. This is actually very simple. Most of you might not have been aware (if not, I showed you earlier) that <code>$('div')[0]</code> and <code>$('div').get(0)</code> return a reference to the first element in the jQuery wrapper object. But, the <code>get()</code> method actually has another purpose. It can quickly turn the DOM elements found in the jQuery wrapper into an array of DOM elements.</p>

<p>Let's return to jQuery.com and open up Firebug again. Inside the console run the following line of code.</p>

<div class="igBar"><span id="ljavascript-61"><a href="#" onclick="javascript:showPlainTxt('javascript-61'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-61">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>You should now be looking at this in the firebug console:</p>

<p><code>"[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]"</code></p>

<p>Essentially the <code>.get()</code> method placed all of the DOM elements inside the jQuery wrapper into an array and returned that array. So now you can use default array methods on your array of elements. For example:</p>

<div class="igBar"><span id="ljavascript-62"><a href="#" onclick="javascript:showPlainTxt('javascript-62'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-62">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span>; <span style="color: #006600; font-style: italic;">//returns the array as a string</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//this is a method of the array object</span></div></li>
</ol></div>
</div></div><br />

<p>Additionally, since we now have an array we can write a statement like this:</p>

<div class="igBar"><span id="ljavascript-63"><a href="#" onclick="javascript:showPlainTxt('javascript-63'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-63">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>; <span style="color: #006600; font-style: italic;">//returns the first div's id attribute value</span></div></li>
</ol></div>
</div></div><br />

<p>There is something worth noting here that might not be obvious to those starting out with jQuery or JavaScript. Since we are now returning an array, the chain is broken. You can't chain any jQuery methods after using the <code>.get()</code> method.</p>

<p>But, heck, you have an array. Now you can use the utility functions that jQuery provides (<code>$.each()</code>, <code>$.grep()</code>, <code>$.map()</code>, <code>$.inArray()</code>, <code>$.unique()</code>), as well as core JavaScript methods and properties, to work on the array. I'm not going to demonstrate all of the jQuery utility functions that can be used on an array, but I do want to examine one of them here in this tutorial.</p>

<p>The <code>$.each()</code> utility function, not to be confused with the <code>$('selector').each()</code> method used to operate on a set of DOM elements in a jQuery wrapper, can be used to seamlessly iterate over an array or object. For example, let's say that you would like to store (or cache) a set of <code>&lt;li&gt;</code> elements in an array, because maybe you are going to remove them from the DOM temporarily but eventually will be placing them back on the page. To do this you could use the <code>$.each()</code> utility function to loop over an array and append each <code>&lt;li&gt;</code> element back to the page. In the code example below this is done on page load by converting and storing the jQuery wrapper of <code>&lt;li&gt;</code> elements to an array, and then looping over this array in order to place them back on the page.</p>

<div class="igBar"><span id="ljavascript-64"><a href="#" onclick="javascript:showPlainTxt('javascript-64'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-64">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;html&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;head&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js&quot;&gt;&lt;/script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/head&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;body&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;ul&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 1&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 2&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 3&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 4&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 5&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 6&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 7&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 8&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/ul&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">var arrayList = $('ul li').get();</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$('ul').empty();</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.each(arrayList, function(){</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$('ul').append('&lt;li&gt;&lt;del&gt;'+$(this).html() +'&lt;/del&gt; complete&lt;/li&gt;');</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">});</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/body&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/html&gt;</div></li>
</ol></div>
</div></div><br />

<p>Notice that I am forgoing the use of the <code>.ready()</code> event by simply placing my jQuery code before the closing body element.</p>

<p>So, hopefully it's obvious how extremely handy it can be at times to make an array out of a jQuery wrapper. In fact, once you are dealing with an array, you can use a handy plugin called <a href="http://plugins.jquery.com/project/rich-array">Rich Array</a>. This plugin provides the following additional utility functions for arrays. And yes, there is a bit of duplication here with some of the core utility functions.</p>

<p>
<strong>$.richArray.in()</strong> //Checks whether an array contains some value<br />
<strong>$.richArray.unique()</strong> //Produces the duplicate-free version of the array<br />
<strong>$.richArray.diff()</strong> //Finds the difference between two arrays.<br />
<strong>$.richArray.intersect()</strong> //Finds the intersection of two arrays<br />
<strong>$.richArray.filter()</strong> //Applies filter to the array, using callback function<br />
<strong>$.richArray.map()</strong> //Applies callback function for each element in the input array, and returns array of values that this function returned<br />
<strong>$.richArray.sum()</strong> //Computes the sum of all array elements<br />
<strong>$.richArray.product()</strong> //Calculates the production of all elements of the array<br />
<strong>$.richArray.reduce()</strong> //Reduces the array. One-element arrays are turned into their unique element<br />
<strong>$.richArray.compact()</strong> //Creates new version of array without null/undefined values<br />
<strong>$.richArray.without()</strong> //Creates a new version of the array that doesn't contain the specified value</p>

[inline][/inline]]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Quick Tip: Dynamically add an icon for external links</title>
		<link>http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links</link>
		<comments>http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links#comments</comments>
		<pubDate>Wed, 20 Aug 2008 03:42:36 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[progressive enhancement]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links</guid>
		<description><![CDATA[A common feature I've seen on &#8220;web 2.0&#8221; sites and wikis is the "external link" icon: . While I'm not crazy about the idea of sticking these little images all over the HTML, they're a great candidate for using progressive enhancement. In our case, we can use jQuery to add the images pretty easily. Test [...]]]></description>
			<content:encoded><![CDATA[<p>A common feature I've seen on &ldquo;web 2.0&rdquo; sites and wikis is the "external link" icon: <img src="/images/external.png" alt="external link" />. While I'm not crazy about the idea of sticking these little images all over the HTML, they're a great candidate for using <a href="http://en.wikipedia.org/wiki/Progressive_Enhancement">progressive enhancement</a>. In our case, we can use jQuery to add the images pretty easily. </p>
<span id="more-103"></span>
<h4>Test the hostname</h4>
<p>To identify the external links, we test for the <code>location.hostname</code> against the link's hostname, which will be represented by <code>this.hostname</code> once we have the selector in place, and make sure the two don't match. We should also test for the mere existence of <code>this.hostname</code> to avoid problems or false positives with "mailto" links. our tests will look like this: <code>this.hostname &amp;&amp; location.hostname !== this.hostname</code>.</p> 

<h4>Use the filter function</h4>
<p>Now let's wrap that test in a filter function. For our example, we'll test all links inside an "extlinks" element that match the above test. Here is what it looks like:</p>
<div class="igBar"><span id="ljavascript-66"><a href="#" onclick="javascript:showPlainTxt('javascript-66'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-66">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#extlinks a'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=filter"><span style="">filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> &#038;& <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">!==</span> location.<span style="color: #660066;">hostname</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;img src=&quot;/images/external.png&quot; alt=&quot;external link&quot;/&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>A $(document).ready() is wrapped around the script so that it will execute when the DOM has loaded. Line 4 shows the insertion of the image after each of the external links. </p>

<h4>Demo</h4>
<p>Here is a little demo using the above code. Of the three links, only the second points to a different site. We should see the external-link icon next to it.</p>

<div id="extlinks">
<ul>
<li><a href="http://www.learningjquery.com/2007/06/automatic-page-contents">Automatic Page Contents</a></li>
<li><a href="http://jquery.com/">jQuery.com</a></li>
<li><a href="http://www.learningjquery.com/2008/01/revealing-details-with-jquery">Revealing Details with jQuery</a></li>
</ul>
</div>
<p>So, that's it. Short and sweet.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links/feed</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
		<item>
		<title>Revealing Details with jQuery</title>
		<link>http://www.learningjquery.com/2008/01/revealing-details-with-jquery</link>
		<comments>http://www.learningjquery.com/2008/01/revealing-details-with-jquery#comments</comments>
		<pubDate>Fri, 04 Jan 2008 04:57:57 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2008/01/revealing-details-with-jquery</guid>
		<description><![CDATA[A week or so ago, someone posted a comment on one of my previous articles, asking if I could help her split up the textual content of an element, showing the first part and replacing the second with a link that, when clicked, would reveal the text. This behavior would appear in an FAQ using [...]]]></description>
			<content:encoded><![CDATA[<p>A week or so ago, <a href="/2006/09/slicker-show-and-hide#comment-25489">someone posted a comment</a> on <a href="http://www.learningjquery.com/2006/09/slicker-show-and-hide">one of my previous articles</a>, asking if I could help her split up the textual content of an element, showing the first part and replacing the second with a link that, when clicked, would reveal the text. This behavior would appear in an FAQ using a definition list (<code>&lt;dl&gt;</code>), with each question contained in a <code>&lt;dt&gt;</code> and each answer contained in a <code>&lt;dd&gt;</code>. I soon realized that the solution would be rather involved, so I decided to create a new entry out of it rather than simply answer her question in another comment.</p>

<p>Here is the simple definition list structure that I'll be using for the example:</p>

<span id="more-90"></span>
[inline][/inline]
<div class="igBar"><span id="lhtml-72"><a href="#" onclick="javascript:showPlainTxt('html-72'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-72">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;dl</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;expander&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&lt;dt&gt;&lt;strong&gt;</span>Benedick's Question<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span>...<span style="color: #009900;"><span style="color: #000000;">&lt;/dt&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&lt;dd&gt;&lt;strong&gt;</span>Beatrice's Answer<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span>...<span style="color: #009900;"><span style="color: #000000;">&lt;/dd&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&lt;dt&gt;&lt;strong&gt;</span>Benedick's Question<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span>...<span style="color: #009900;"><span style="color: #000000;">&lt;/dt&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&lt;dd&gt;&lt;strong&gt;</span>Beatrice's Answer<span style="color: #009900;"><span style="color: #000000;">&lt;/strong&gt;</span></span>...<span style="color: #009900;"><span style="color: #000000;">&lt;/dd&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/dl&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<h4>First Steps</h4>
<p>As usual, we start the script with a "document.ready" line so that it can be fired when the DOM has finished loading. Next comes our primary selector expression. For this example, I've given the targeted <code>&lt;dl&gt;</code> element a class of "expander" so that we don't inadvertently attach the behavior to other <code>&lt;dl&gt;</code>s. And since we're manipulating the contents of each <code>&lt;dd&gt;</code> independently of one another, we can make things happen inside an <code>.each()</code> method:</p>
<div class="igBar"><span id="ljavascript-73"><a href="#" onclick="javascript:showPlainTxt('javascript-73'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-73">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=index"><span style="">index</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>We ought to declare some variables now. The first two will go right after the document.ready line, while the others will appear inside the <code>.each()</code> method because they change with each <code>&lt;dd&gt;</code> element.</p>
<ul>
  <li>slicePoint (integer): the number of characters at which the contents will be sliced into two parts. Note: any tag names in the HTML that appear inside the <code>&lt;dd&gt;</code> before the slicePoint will be counted along with the text characters. </li>
  <li>widow (integer): this is a threshold of sorts for whether we want to initially hide part of the <code>&lt;dd&gt;</code>'s contents. If after slicing the contents in two there are fewer words in the second part than the value set by widow, we won't bother hiding anything.</li>
</ul>
<ul>
  <li>allText (string): the full contents of the <code>&lt;dd&gt;</code></li>
  <li>startText (string): the first part of the <code>&lt;dd&gt;</code>'s contents — the part that is immediately visible. First, all characters up to the slicePoint are included, and then any "word" characters at the end of the string are removed to avoid ending with a partial word. Keep in mind that in almost every case this approach will leave us with fewer characters than we indicated in the slicePoint variable</li>
  <li>endText (string): the second part of the <code>&lt;dd&gt;</code>'s contents. This part is initially hidden from view, replaced by a "read more..." link.</li>
</ul>

<p>Here is what we have so far:</p>

<div class="igBar"><span id="ljavascript-74"><a href="#" onclick="javascript:showPlainTxt('javascript-74'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-74">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> slicePoint <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">100</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> widow <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">4</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> allText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startText <span style="color: #339933;">=</span> allText.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #339933;">,</span>slicePoint<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\w+$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endText <span style="color: #339933;">=</span> allText.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>startText.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Although jQuery has its own <code>.slice()</code> method, which operates on a jQuery object, we're using JavaScript's native method, which can operate on both strings (as is the case here) and arrays. Our first use of <code>.slice()</code>, declaring the startText variable, has two arguments — one for the beginning position of the string and one for the ending. Our second has only one argument, for the starting position; the (allText) string's length is the implied ending position. Also, note that for startText, we're <em>chaining</em> <code>.slice()</code> and the <code>.replace()</code> regular expression method.</p>
<h4>Manipulating the Contents</h4>
<p>Now that we have our two blocks of content, we're going to add a "read more..." link after the startText and wrap the endText in a span (with a class of "details"), but only if there are more words than the value defined by the widow variable. (tangent: am I the only one who can't type "widow" right the first time, always typing "window" instead?). There are many ways to create new elements and insert them into the DOM with jQuery. For this example, we're going to use the <code>.html()</code> method, create an array of strings inside it, and join those strings.</p>

<div class="igBar"><span id="ljavascript-75"><a href="#" onclick="javascript:showPlainTxt('javascript-75'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-75">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> slicePoint <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">100</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> widow <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">4</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> allText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startText <span style="color: #339933;">=</span> allText.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #339933;">,</span>slicePoint<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\w+$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endText <span style="color: #339933;">=</span> allText.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>startText.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// *** commence DOM manipulation ... </span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> endText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s+$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span> widow <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; startText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;a href=&quot;#&quot; class=&quot;read-more&quot;&gt;read more...&lt;/a&gt;'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;span class=&quot;details&quot;&gt;'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;/span&gt;'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#93;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>I picked up that technique of creating an array and joining its elements from the <a href="http://groups/google.com/group/jquery-en/">jQuery Google Group</a>. It seems cleaner than string concatenation somehow, and rumor has it that it's a bit faster, too.</p>

<h4>Finishing Touches</h4>
<p>We have everything in place now, so all we need to do is hide the content inside our newly created <code>&lt;span class="details"&gt;</code> and attach a click handler to the "read more" link. Here is the completed code:</p>

<div class="igBar"><span id="ljavascript-76"><a href="#" onclick="javascript:showPlainTxt('javascript-76'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-76">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> slicePoint <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">100</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> widow <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">4</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> allText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startText <span style="color: #339933;">=</span> allText.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #339933;">,</span>slicePoint<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\w+$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endText <span style="color: #339933;">=</span> allText.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>startText.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> endText.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s+$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span> widow <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; startText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;a href=&quot;#&quot; class=&quot;read-more&quot;&gt;read more...&lt;/a&gt;'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;span class=&quot;details&quot;&gt;'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;/span&gt;'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#93;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// *** hide details until read-more link is clicked;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #006600; font-style: italic;">// then hide link and show details. </span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.details'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.read-more'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.details'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=fadeIn"><span style="">fadeIn</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>I used a <code>.fadeIn()</code> because I like the way it looks, but a simple <code>.show()</code> would do. The <code>return false;</code> line is important to prevent a jump to the top of the page on click. </p>

<h4>the Demo</h4>
<p>Here is a little demo:</p>

 <dl class="expander">
  <dt><strong>Benedick's Question</strong>: I pray you, what is he?</dt>
  <dd><strong>Beatrice's Answer</strong>: Why, he is the prince's jester: a very dull fool; only his gift is in devising impossible slanders: none but libertines delight in him; and the commendation is not in his wit, but in his villany; for he both pleases men and angers them, and then they laugh at him and beat him. I am sure he is in the fleet: I would he had boarded me. (<a href="http://shakespeare.mit.edu/much_ado/much_ado.2.1.html"><i>Much Ado About Nothing</i>, II.1</a>)
  </dd>

<dt><strong>Benedick's Question</strong>: I do love nothing in the world so well as you: is not that strange?</dt>

  <dd><strong>Beatrice's Answer</strong>: As strange as the thing I know not. It were as possible for me to say I loved nothing so well as you: but believe me not; and yet I lie not; I confess nothing, nor I deny nothing. (<a href="http://shakespeare.mit.edu/much_ado/much_ado.4.1.html"><i>Much Ado About Nothing</i>, IV.1</a>)
  </dd>
  <dt><strong>Karl's Question</strong>: Why aren't you using another question from <i>Much Ado About Nothing</i> here?</dt>  
  <dd><strong>Karl's Answer</strong>: Because I'm lazy. And besides, this demo is getting a little too pretentious.</dd>
 </dl>
 ]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/01/revealing-details-with-jquery/feed</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Questions and Answers from the List</title>
		<link>http://www.learningjquery.com/2007/12/questions-and-answers-from-the-list</link>
		<comments>http://www.learningjquery.com/2007/12/questions-and-answers-from-the-list#comments</comments>
		<pubDate>Thu, 20 Dec 2007 02:13:25 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2007/12/questions-and-answers-from-the-list</guid>
		<description><![CDATA[I've been feeling guilty lately about my lack of posts to this blog. But when I looked at my profile for the jQuery Google Group and discovered that for the past six months I've posted an average of 100+ times each month, well, I decided to give myself a break. Since I'm sure some people [...]]]></description>
			<content:encoded><![CDATA[<p>I've been feeling guilty lately about my lack of posts to this blog. But when I looked at my profile for the <a href="http://groups.google.com/group/jquery-en">jQuery Google Group</a> and discovered that for the past six months I've posted an average of 100+ times each month, well, I decided to give myself a break. Since I'm sure some people who stumble upon this blog aren't subscribed to the Google group/mailing list, here are a few (edited) questions that have appeared there recently, along with my (edited) answers. I hope some of you find them helpful.</p>
<span id="more-87"></span>
<h4>Question: Filter Function</h4>
<blockquote>

<p>I have this HTML:</p>

<div class="igBar"><span id="lhtml-85"><a href="#" onclick="javascript:showPlainTxt('html-85'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-85">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;p&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;b&gt;</span></span>Text1<span style="color: #009900;"><span style="color: #000000;">&lt;/b&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/p&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;b&gt;</span></span>Text2<span style="color: #009900;"><span style="color: #000000;">&lt;/b&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>How can I select the second <code>&lt;b&gt;</code>, which is not a child of a <code>&lt;p&gt;</code>? As I am a newbie, I played a little bit around with <code>:not</code>, <code>filter()</code>, but unfortunately...</p>

<div class="igBar"><span id="ljavascript-86"><a href="#" onclick="javascript:showPlainTxt('javascript-86'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-86">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'b'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=filter"><span style="">filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:not(p&gt; b)&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// both &lt;b&gt; are selected.</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'b'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=not"><span style="">not</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p&gt; b'</span><span style="color: #009900;">&#41;</span> &nbsp; <span style="color: #006600; font-style: italic;">// both &lt;/b&gt;&lt;b&gt; are selected.</span></div></li>
</ol></div>
</div></div><br />

<p>Any hints?</p>

</b></blockquote>
<h4>Answer</h4>
<p>Try this:</p>
  
<div class="igBar"><span id="ljavascript-87"><a href="#" onclick="javascript:showPlainTxt('javascript-87'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-87">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'b'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=filter"><span style="">filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">!</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This finds all <code>&lt;b&gt;</code> elements and then filters them. The filter returns only those that do not (<code>!</code>) have a parent paragraph ( <code>$(this).parent('p').length</code> ). </p>
<h4>Question: Filter by Option Value</h4>
<blockquote><p>I have several options in a select element, each whose value is an integer. I would love to be able to select those above a threshold, such as something like:</p>

<pre><code>$("#mySelect option[value>5]")</code></pre>

<p>I know I can do this with <code>:gt(index)</code> but I'd rather use the actual value for less hard coding and better readability.  I think this would be great in other areas too.</p>

<p>I'm sure there are lots of issues with this since the value is really just text, but a straight string comparison would probably do.</p>
</blockquote>
<h4>Answer</h4>
  
<p>You could try something like this:</p>

  <div class="igBar"><span id="ljavascript-88"><a href="#" onclick="javascript:showPlainTxt('javascript-88'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-88">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mySelect option'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=filter"><span style="">filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#41;</span><span style="">&gt;</span> <span style="color: #CC0000;color:#800000;">5</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
  
<p>This should filter the <code>option</code> elements, selecting only those whose value is greater than 5.</p>

<h4>Question: Puzzle of "this"</h4>
<blockquote>  
<p>I am puzzled by the use of <code>this</code> somehow. For example:</p>

<div class="igBar"><span id="ljavascript-89"><a href="#" onclick="javascript:showPlainTxt('javascript-89'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-89">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.delete'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.post"><span style="">$.<span style="color: #660066;">post</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test2.php&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span>txt<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">something</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Here, if I want to refer this to the <code>$('.delete')</code> element, what shall I write? In the debugger I found that <code>this</code> refers to the function body.</p>
</blockquote>

<h4>Answer</h4>

<p>You could try this:</p>

<div class="igBar"><span id="ljavascript-90"><a href="#" onclick="javascript:showPlainTxt('javascript-90'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-90">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.delete'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $thisDelete <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>; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.post"><span style="">$.<span style="color: #660066;">post</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test2.php&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span>txt<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; $thisDelete.<span style="color: #660066;">somejQueryMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The <code>$thisDelete</code> variable is now referencing the clicked element wrapped in a jQuery object. This is useful for attaching jQuery methods, chaining, etc. Oh, and by the way, the use of the "$" in <code>$thisDelete</code> isn't necessary, but it has become somewhat of a convention among jQuery developers, as it reminds us that we're dealing with a jQuery object.
</p>
<p>Now, when you use <code>$thisDelete</code> inside your <code>$.post</code>, it's still referring to the clicked element.</p>
<h4>Question: Swap an Image on Click</h4>
<blockquote><p>I have a simple accordion set up, but I want to be able to change the header's background image from an up arrow state to down arrow state and back automatically. Any ideas on where to go from here?</p></blockquote>

<h4>Answer</h4>

<p>After the <code>.click()</code> line [which slides the following <code>div</code> up or dow], you can do something like this...</p>

<div class="igBar"><span id="ljavascript-91"><a href="#" onclick="javascript:showPlainTxt('javascript-91'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-91">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> currImage <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'backgroundImage'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>backgroundImage<span style="color: #339933;">:</span> currImage <span style="color: #339933;">==</span> <span style="color: #3366CC;">'up-arrow.jpg'</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'down-arrow.jpg'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'up-arrow.jpg'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The first line here stores the clicked header's background image in the "currImage" variable. The second line then sets the background image. It uses a ternary, or conditional, operator to check if the background image is "up-arrow.jpg". If it is, the image is set to "down-arrow.jpg"; if it isn't "up-arrow.jpg", the image is set to "up-arrow.jpg".</p>

<h4>Question: How to Center a DIV Vertically in the Viewport</h4>
<blockquote>
<p>Thing is I got a project on which i want to show a preloader whenever something is sent or some event has been triggered. How could I set the preloader to show itself right on the middle of the screen?? I mean how could I show it on the right middle of the screen even if I got a huge scrollbar on the right side!?</p>
</blockquote>  
<h4>Answer</h4>

  
<p>So here is one way you could get vertical centering in the viewport [with sTop code borrowed from an old copy of Dimensions plugin]:</p>

<div class="igBar"><span id="ljavascript-92"><a href="#" onclick="javascript:showPlainTxt('javascript-92'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-92">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// vertically center something in the viewport</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// make it a plugin!</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">vCenter</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></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> pos <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; sTop <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #660066;">pageYOffset</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> document.<span style="color: #660066;">documentElement</span> &#038;& document.<span style="color: #660066;">documentElement</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=scrollTop"><span style="">scrollTop</span></a> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> &nbsp;document.<span style="color: #660066;">body</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=scrollTop"><span style="">scrollTop</span></a>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; wHeight <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> window.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=innerHeight"><span style="">innerHeight</span></a> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> document.<span style="color: #660066;">documentElement</span> &#038;& document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">clientHeight</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> document.<span style="color: #660066;">body</span>.<span style="color: #660066;">clientHeight</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=index"><span style="">index</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><a href="/wp-content/themes/ljq/docs-1.7.php?fn=index"><span style="">index</span></a> <span style="color: #339933;">==</span> <span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <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>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> elHeight <span style="color: #339933;">=</span> $this.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=height"><span style="">height</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> elTop <span style="color: #339933;">=</span> pos.<span style="color: #660066;">sTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>pos.<span style="color: #660066;">wHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009966; font-style: italic;">/ 2) - (elHeight /</span> <span style="color: #CC0000;color:#800000;">2</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; $this.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="/wp-content/themes/ljq/docs-1.7.php?fn=position"><span style="">position</span></a><span style="color: #339933;">:</span> <span style="color: #3366CC;">'absolute'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marginTop<span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> elTop</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #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: #006600; font-style: italic;">// end plugin</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// center an element - could go in your custom js file</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#foo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">vCenter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Take a look at the <a href="http://test.learningjquery.com/center.html">demo page I put together</a>. Click on a "center" button, and see where the div goes. Scroll down and click on another button. The div still goes to the center of the viewable area.</p>

<p><strong>UPDATE:</strong> Because this didn't work properly in Opera 9.5 as reported in <a href="/2007/12/questions-and-answers-from-the-list#comment-24823">comment #4 below</a>, I refactored the code for returning the scroll top and window height values. Please continue to report any errors that you discover. I also welcome suggestions for improvements to any of the code here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2007/12/questions-and-answers-from-the-list/feed</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (User agent is rejected)
Database Caching 9/48 queries in 0.034 seconds using disk: basic
Object Caching 681/780 objects using disk: basic
Content Delivery Network via learningjquery.kswedberg.netdna-cdn.com

Served from: www.learningjquery.com @ 2012-02-08 14:05:38 -->
