<?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 Modification</title>
	<atom:link href="http://www.learningjquery.com/category/types/dom-modification/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.com</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Sat, 06 Mar 2010 18:10:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple Tooltip for Huge Number of Elements</title>
		<link>http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements</link>
		<comments>http://www.learningjquery.com/2009/12/simple-tooltip-for-huge-number-of-elements#comments</comments>
		<pubDate>Tue, 01 Dec 2009 17:57:29 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[event delegation]]></category>
		<category><![CDATA[live]]></category>
		<category><![CDATA[tooltips]]></category>

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

		<guid isPermaLink="false">http://www.learningjquery.com/?p=542</guid>
		<description><![CDATA[The .append() method is perhaps the most misused of all jQuery methods. While an extremely useful and easy method to work with, it dramatically affects the performance of your page. When misused, the .append() method can cripple your JavaScript code's performance. When used well, it'll keep your script humming along.   
[inline] [/inline]
Here is [...]]]></description>
			<content:encoded><![CDATA[<p>The <code>.append()</code> method is perhaps the most misused of all jQuery methods. While an extremely useful and easy method to work with, it dramatically affects the performance of your page. When misused, the <code>.append()</code> method can cripple your JavaScript code's performance. When used well, it'll keep your script humming along.  </p> <span id="more-542"></span>
[inline] [/inline]
<p>Here is a typical example of append misuse:</p>

<div class="igBar"><span id="ljavascript-16"><a href="#" onclick="javascript:showPlainTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-16">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> reallyLongArray;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> newTd <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;td&gt;&lt;/td&gt;'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'pieTD'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> newTr <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;tr&gt;&lt;/tr&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; newTr.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span>newTd<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span>newTr<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><em>Ran Once: Profile (308.697ms, 17861 calls, Firefox 3.06)<br />
Ran in a for loop 100 times: Profile (51782.295ms, 1805100 calls)</em></p>

<p>If you're coming from Prototype to jQuery, chances are this looks quite natural to you. It's the way that Prototype does node creation/insertion, but with a little bit of jQuery chaining thrown in. Let's see how we can improve this.</p>

<div class="igBar"><span id="ljavascript-17"><a href="#" onclick="javascript:showPlainTxt('javascript-17'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-17">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> reallyLongArray; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> newTr <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td name=&quot;pieTD&quot;&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/td&gt;&lt;/tr&gt;'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span>newTr<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><em>Ran Once: Profile (107.458ms, 3991 calls, Firefox 3.06)<br />
Loop of 100: Profile (21641.336ms, 399100 calls)</em></p>

<p>Whoa! <strong>Nearly a 3x difference</strong>, and much simpler to look at. jQuery can take more then one node at a time and create them all at once. You also don't need to wrap it in <code>$()</code> before appending it. jQuery knows what to do. But wait, there's more!</p>

<div class="igBar"><span id="ljavascript-18"><a href="#" onclick="javascript:showPlainTxt('javascript-18'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-18">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> reallyLongArray; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> textToInsert <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert &nbsp;<span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td name=&quot;pieTD&quot;&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/td&gt;&lt;/tr&gt;'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span>textToInsert<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><em>Ran Once: Profile (30.792ms, 778 calls, Firefox 3.06) <br />
Loop of 100: Profile (8505.37ms, 77800 calls)</em></p>

<p>Taking full advantage of the ability of jQuery to insert a chunk of html in a string means only having to call insert once. It's much quicker, with an <strong>approximately 9-10x speed increase</strong> from the initial algorithm!  This will be fast enough for 95% of cases, but for strings with lots of string concatenation&hellip; Wait, there's more!</p>

<div class="igBar"><span id="ljavascript-19"><a href="#" onclick="javascript:showPlainTxt('javascript-19'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-19">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> reallyLongArray; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> textToInsert <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">0</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td name=&quot;pieTD&quot;&gt;'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;/td&gt;&lt;/tr&gt;'</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span>textToInsert.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Credit for this one goes to <a href="http://groups.google.com/group/jquery-en/browse_thread/thread/9889ebd5e10c9122">Michael Geary</a><br />
<em>Ran Once: Profile (29.724ms, 778 calls, Firefox 3.06)<br />
Loop of 100:  Profile (8298.699ms, 77800 calls)</em></p>

<p>This version is a bit harder to understand, as the html is not in an easy to read format, and the results vary by browser* (see below for further analysis), but in most current and next generation browsers, adding to an array and joining it into a string at the end is quicker then doing string concatenations. So, if you are looping through a large number of string concatenation and need some more speed, you should consider this method. So, now we're done, right?  This is as blazingly fast as we can get it?  Not quite.</p>

<div class="igBar"><span id="ljavascript-20"><a href="#" onclick="javascript:showPlainTxt('javascript-20'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-20">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> reallyLongArray; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> textToInsert <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">0</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">=</span> arr.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">0</span>; a <span style="color: #339933;">&lt;</span>length; a <span style="color: #339933;">+=</span> <span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td name=&quot;pieTD&quot;&gt;'</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> arr<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; textToInsert<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;/td&gt; &lt;/tr&gt;'</span> ;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span>textToInsert.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><em>Ran Once: Profile (29.72ms, 587 calls, Firefox 3.06)<br />
Loop of 100: Profile (8274.359ms, 58700 calls)</em></p>

<p>As handy as <code>.each()</code> is for small loops, calling a function that executes a callback inside of a loop will always be slower than just creating the loop in pure JavaScript. The difference here isn't so noticeable because the array I used only has a length of 190, but for really large loops it makes a measurable difference.  In addition, while the difference is very slight (.000062 vs .000037 ms over 1,000,000 empty loops), it is quicker to save the array length in a variable and use it instead of looking up an object property every loop (thanks Karl Swedberg!).</p>

<p>For most of your uses, the method of creating one really long string and appending it at the end will be the best choice, as it makes the best use of the trade offs of code legibility, ease of programming, and speed.</p>

<p>Please tell me your methods for speeding up array loops or appending using jQuery in the comments below.</p>

<p>For completeness, I used jQuery 1.3.2, Firebug 1.3.3 and Fireunit 1.0a1 on Firefox 3.06 on a Mac to do the profiling. And the <em>reallyLongArray</em> I used was:</p>

<pre><code>var arr = [1, 2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 'ffadscdasc', 'cada', 
'2wd', 3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 'ffadscdasc', 
'cada', '2wd', 3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 
'ffadscdasc', 'cada', '2wd', 3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 
6, 2, 3, 6, 'ffadscdasc', 'cada', '2wd', 3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 
'adsf', 'as', 6, 2, 3, 6, 'ffadscdasc', 'cada', '2wd', 3, 7, 3, 2, 'sdf', 1, 
2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 'ffadscdasc', 'cada', '2wd', 3, 7, 3, 
2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 'ffadscdasc', 'cada', '2wd', 
3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 'ffadscdasc', 
'cada', '2wd', 3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 6, 2, 3, 6, 
'ffadscdasc', 'cada', '2wd', 3, 7, 3, 2, 'sdf', 1, 2, 3, 4, 5, 'adsf', 'as', 
6, 2, 3, 6, 'ffadscdasc', 'cada', '2wd', 3, 7, 3, 2, 'sdf'];
</code></pre>





<p><h2>Further analysis of += vs array.join, broken down by browser</h2></p>

<pre>---------------</pre>

<strong>Current gen Browsers</strong><pre>
FF3 		array.join is ~2x faster
Safari 3 	array.join ~1.5x faster
Opera 9		+= ~3x faster
ie6		array.join ~6x faster
ie7		array.join ~4x faster</pre>

<strong>Next gen browsers</strong><pre>
FF3.1		+= array.join equal in speed
Chrome		+=  ~1.25x faster
IE8		array.join ~1.6x faster
Webkit		array.join ~2x faster</pre>



<h4>Raw results</h4>
<strong>(2709 characters) 9 character string, added 300 times.  Looped 100,000 times and averaged.</strong>
+=<pre>
FF3.06 		.2046  -  .25835 .1849  .17045
Opera9 		.1256  -  .12795 .12678 .12197
Safari3 	.1368  -  .14875 .13128 .13032
IE6 		3.359 - took forever, crashed/froze browser sometimes
IE7 		1.68985 - took forever

FF3.1		.0735  -  .0813 .0710 .0683
Chrome 		.0256  -  .025 .021 .031
Webkit 		.0655  -  .06602 .06527 .06533
IE8		.2921  -  .3104 .2905 .2755 </pre>

<strong>array.join - called 300 times, looped 100,000 times and averaged.</strong><pre>
FF3.06 		.1056  -  .10701 .10439 .10541
Opera9 		.3355  -  .33091 .31484 .36066
Safari3 	.0813  -  .08128 .08089 .08172
IE6  		.5086  -  .50609 .50829 .51156
IE7 		.4101  -  .42265 .38875 .41891

FF3.1		.0743  -  .0766 .0721 .0741
Chrome 		.0315  -  .03 .048 .027 .021
Webkit 		.0300  -  .02962 .03123 .02937
IE8		.1832  -  .1841.1872 .1872 .1741</pre>


<strong>(35109 character string) 351 character string added to a nine character string 100 times, looped 1000 times and averaged</strong>
+= <pre>
FF3.06 		1.81	-   1.897 2.015 1.518
Opera9 		.6306	-   .631 .634 .627
Safari3 	.9156	-   .931 .884 .932
IE6 		65.657	-   58.078 56.641 89.313 58.594  // I can see where += was a huge problem then.
IE7 		.9316	-   .937 .934 .924

FF3.1		.9141	-   .47 .663 1.123 .705 1.165 1.361 .698 1.128
Chrome 		.031	-   .039 .024 .02 .041
Webkit 		.996	-   .991 1.014 .983
IE8		.09	-   .094 .094 .078 .094 </pre>

<strong>array.join the above</strong><pre>
FF3.06		.9143	-  .923  .83 .99
Opera9		1.773	-  1.832 1.747 1.741
Safari3		.2106	-  .249 .19 .193
IE6		.8368	-  2 .375 .469 2.078 .343 .36 .735 .625 .547
IE7		.4428	-  .531 .516 .391 .375 .438 .406 .609


FF3.1		1.070	-  1.221 .678 1.366 1.508 .665 1.423 .631
Chrome		.0318	-  .02 .022 .059 .026
Webkit		.1773	-  .175 .176 .181
IE8		.4820	-  .14 1.141 .156 .609 .625 .328 .375</pre>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly/feed</wfw:commentRss>
		<slash:comments>58</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-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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#extlinks a'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>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>67</slash:comments>
		</item>
		<item>
		<title>Three Quick Ways to Avoid Widows</title>
		<link>http://www.learningjquery.com/2008/07/three-quick-ways-to-avoid-widows</link>
		<comments>http://www.learningjquery.com/2008/07/three-quick-ways-to-avoid-widows#comments</comments>
		<pubDate>Tue, 08 Jul 2008 17:44:01 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2008/07/three-quick-ways-to-avoid-widows</guid>
		<description><![CDATA[A few months ago I threw together a quick redesign of the Learning jQuery site. It's nothing fancy, mind you, but I was itching to retire the thin veil covering the tired old WordPress Kubrick theme, so something had to be done. 

Almost immediately upon changing the font-family and font-size of the blog post titles, [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago I threw together a quick redesign of the Learning jQuery site. It's nothing fancy, mind you, but I was itching to retire the thin veil covering the tired old WordPress Kubrick theme, so something had to be done. </p>

<p>Almost immediately upon changing the font-family and font-size of the blog post titles, I noticed a few unsightly widows (just to clarify, we're talking about <a href="http://en.wikipedia.org/wiki/Widows_and_orphans"><em>typographical</em> widows</a>. My mother already suspects me of avoiding her; I don't want to add to her anxiety. ;) ). </p>

<p>Here is an example of one such widow:  <span id="more-93"></span></p>
<p><img src="/images/widow.png" alt="widow" /></p>

<p>See how the last word, "plugin," appears on its own line? According to a couple designerly friends of mine, that's a no-no. So, I considered for half a minute how to get that title to look more like this:</p>
<p><img src="/images/nowidow.png" alt="no widow" /></p>

<p>The lowly yet lovely non-breaking space (&amp;nbsp;) would do the trick, but how to replace it for the regular, breaking space? I certainly wasn't about to manually edit all of the entries' titles. Not only would it take too long, but it would also pollute the markup with something that really shouldn't be there. No, what I needed was a little JavaScript. </p>

<h4>Selecting the Titles</h4>
<p>On this site, entry titles are wrapped in <code>&lt;h2&gt;&lt;a href=&quot;foo&quot;&gt;&lt;/a&gt;&lt;/h2&gt;</code>, which can be selected in jQuery with <code>$('h2 a')</code>. Easy. Now, because I want to manipulate the text of each title independently, I'll need to use the <code>.each()</code> method, which is basically a chainable <code>for</code> loop. Inside the <code>.each()</code> is where I substitute the last breaking space with the non-breaking variety. Here are three ways I came up with to achieve this.</p>

<h4>Array</h4>
<p>The first approach was to convert the title string into an array of words and then stitch the array items back together, dealing with the last one specially.</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> h2Text <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h2 a'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> h2Array <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<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: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; h2Last <span style="color: #339933;">=</span> h2Array.<span style="color: #660066;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; h2Text <span style="color: #339933;">=</span> h2Array.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&nbsp;'</span> <span style="color: #339933;">+</span> h2Last;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span>h2Text<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; &nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>A couple things to note about line 5 above: (a) the variable is actually being declared (with a "var") because it's separated from the previous variable declaration by a comma rather than a statement-ending semicolon. (b) The JavaScript <code>.pop()</code> array method "pops" the last item off the array and returns it; so it's no longer part of <code>h2Array</code>, but its value is stored in the <code>h2Last</code> variable. This is especially handy for us, because we don't want the last word to appear twice. </p>

<p>Line 6 joins the remaining array items with a space between them and then appends the non-breaking space and the popped last item. Line 7 dumps that concatenated string back into the title, inside <code>&lt;h2&gt;&lt;a&gt;&lt;/a&gt;&lt;/h2&gt;</code>.</p>

<h4>String</h4>
<p>The next approach involved working solely with strings, using the <code>slice</code> and <code>lastIndexOf</code> methods to split the the string into two pieces &mdash; one leading up to the last space, and one immediately following the last space.</p>

<div class="igBar"><span id="ljavascript-27"><a href="#" onclick="javascript:showPlainTxt('javascript-27'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-27">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> h2all<span style="color: #339933;">,</span> h2a<span style="color: #339933;">,</span> h2b;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h2 a'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; h2all <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; h2a <span style="color: #339933;">=</span> h2all.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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> h2all.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; h2b <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&nbsp;'</span> <span style="color: #339933;">+</span> h2all.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>h2all.<span style="color: #660066;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span>h2a <span style="color: #339933;">+</span> h2b<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>As line 7 demonstrates, the two sliced strings are stitched back together to keep the last two words on the same line.</p>

<h4>Regular Expression</h4>
<p>The final technique is the one I ended up sticking with, partly because it's the tersest and partly because I have a fondness for regular expressions:</p>

<div class="igBar"><span id="ljavascript-28"><a href="#" onclick="javascript:showPlainTxt('javascript-28'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-28">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> h2Text <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'h2 a'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; h2Text &nbsp;<span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<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;">'&nbsp;$1'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span>h2Text<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>The distinguishing feature here is line 4, which uses the <code>replace</code> regular-expression method. This method takes two arguments, a pattern to match against and a replacement value. For the pattern, which appears between the two slashes, we first match a space and then match one or more "word characters" (letters, numerals, or underscores). The parentheses capture all but that initials space, and the "$" at the end ensures that the match appears at the end of the string. The replacement argument starts with the non-breaking space and follows with <code>$1</code>, which refers to the first (and in our case, only) parenthetical "capture group." (Please forgive me if I've provided too much detail about the regular expression. I'm never quite sure how much of this stuff is worth mentioning, but since this entry is targeting beginners, I suppose it's better to err on the side of too much.)</p>

<div class="update">
<h4>Update</h4>
<p><ins datetime="2008-07-09T23:15:54+00:00">A few people pointed out in the comments that my regular expression could be improved, and I agree. In particular, as noted by Art Lawry, the <code>\w</code> can be changed to <code>\S</code> (that's an <em>uppercase</em> S) to match any non-whitespace character. That way it'll match characters such as &ouml; and &ccedil; as well.</ins></p>
</div>
<p>By the way, all three of these code examples can be reduced in length quite a bit. For example, the regular expression example can be pared down from 7 to 5 lines if we don't bother with the <code>h2Text</code> variable and instead do something like this:</p>
<p><code>$(this).html($(this).text().replace(/ (\w+)$/,'&nbsp;$1'));</code></p>
<p>However, the code is usually easier to read and maintain (for me, at least) if the value is first stored in a variable.</p>
<p>Any suggestions for improvement here? Any other approaches that you would recommend instead? Leave a comment.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/07/three-quick-ways-to-avoid-widows/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Using Low Pro for jQuery</title>
		<link>http://www.learningjquery.com/2008/05/using-low-pro-for-jquery</link>
		<comments>http://www.learningjquery.com/2008/05/using-low-pro-for-jquery#comments</comments>
		<pubDate>Mon, 12 May 2008 12:39:50 +0000</pubDate>
		<dc:creator>Tane Piper</dc:creator>
				<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2008/05/using-low-pro-for-jquery</guid>
		<description><![CDATA[Recently I have been getting a real buzz out of developing with jQuery. I've been using the library since 2006, releasing sporadic bits of code. In April of this year, I released the third revision of my most complex plugin, jMaps, and updated several other plugins, which are available in my mercurial repository.

This was also [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been getting a real buzz out of developing with <a href="http://jquery.com" title="jQuery Homepage">jQuery</a>. I've been using the library since 2006, releasing sporadic bits of code. In April of this year, I released the third revision of my most complex plugin, <a href="http://jmaps.digitalspaghetti.me.uk" title="jMaps Map Framework">jMaps</a>, and updated several other plugins, which are available in my <a href="http://hg.digitalspaghetti.me.uk">mercurial repository</a>.</p>

<p>This was also the same month I discovered a new plugin which has dramatically changed how I develop applications with jQuery. The plugin in question is Dan Webb's <a href="http://github.com/danwrong/low-pro-for-jquery/">Low Pro for jQuery</a>, a port of the plugin of the same name for <a href="http://www.prototypejs.org/">Prototype</a>.</p>		

<h4>What is Low Pro?</h4>
<p>So what is Low Pro? It's a plugin that provides a way of making more object-oriented JavaScript through event delegation. jQuery's plugin architecture provides a really simple way of extending the core functionality, but there is no easy way of making macros of code that do several types of events on one element. Until now!</p>
<span id="more-95"></span>
		
<p>Probably the simplest way to explain how Low Pro works is to build a quick demonstration. Sometimes I find that too many tutorials out there expect prior knowledge, so with that in mind, I will try to make the tutorial as simple as possible. However, this example does use another couple of plugins that are outside the scope of this tutorial:</p>
		<ul>
			<li>
				<a href="http://brandonaaron.net/docs/livequery/">Live Query</a> - Low Pro uses this plugin to make binding to dynamic elements in the DOM easier.
			</li>
			<li>
				<a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validation</a> - We'll use this plugin to show how we can bind other plugin functions to events.
			</li>
		</ul>			
   <p> Both these plugins already have excellent documentation, so if you wish to extend this example, please check them out.</p>
			
<h4>Example 1: Creating a re-usable registration form</h4>
<p>For this example we'll build a simple user registration form. This is something that is common enough on the web, but a nice advantage of this example is that it can be used over and over again with little or no modification. Hopefully once you have learned this pattern, you will be able to see lots of other applications that can be reused over and over again in the same fashion.</p>
		
<p>Our first step is to create our basic form:</p>
		
<div class="igBar"><span id="lhtml-38"><a href="#" onclick="javascript:showPlainTxt('html-38'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-38">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; </span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #00bbdd;"> &nbsp;&quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;html&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;head&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=iso-8859-1&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;title&gt;</span></span>Lowpro Example<span style="color: #009900;"><span style="color: #000000;">&lt;/title&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/jquery.js&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/jquery.livequery.js&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/lowpro.jquery.js&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/jquery.validate.js&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/reg-form.js&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/head&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;body&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form-container&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;.&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lowpro-form&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;fieldset&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;legend&gt;</span></span>User Details<span style="color: #009900;"><span style="color: #000000;">&lt;/legend&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;ul&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span><span style="color: #000000;">&gt;</span></span>Username<span style="color: #009900;"><span style="color: #000000;">&lt;/label&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #000000;">&gt;</span></span>Email<span style="color: #009900;"><span style="color: #000000;">&lt;/label&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password1&quot;</span><span style="color: #000000;">&gt;</span></span>Password<span style="color: #009900;"><span style="color: #000000;">&lt;/label&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password1&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password1&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;username&quot;</span><span style="color: #000000;">&gt;</span></span>Password (Again)<span style="color: #009900;"><span style="color: #000000;">&lt;/label&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password2&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;password2&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;register&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Register&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/fieldset&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/form&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/body&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/html&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Now, we create our JavaScript file called <code>reg-form.js</code>. In here, we will create our class that we'll attach to the form. Inside this class, we'll create some functions that trigger on specific events. Every class created with Low Pro takes the form of <code>$.klass({...});</code>. Inside the curly braces, we use JavaScript prototype nature to create a pseudo-class. We will call our example <strong>RegisterForm</strong>.</p>
		
<div class="igBar"><span id="ljavascript-39"><a href="#" onclick="javascript:showPlainTxt('javascript-39'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-39">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">RegisterForm <span style="color: #339933;">=</span> $.<span style="color: #660066;">klass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; initialize <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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>If you have developed in languages like PHP, Python, or Java, you've probably seen something like this before. This first function, <code>initialize</code>, is a reserved function in Low Pro that accepts one parameter&mdash;an options parameter (we'll discuss that later). This function is always called whenever you bind a class to an element. Because it does not require any event to trigger, it is very useful for setting up initial data or events on dynamic DOM elements.</p>

<h4>Validation</h4>
<p>The first thing we'll do is create our JavaScript validation rules. This tutorial does not cover the creation of these rules; however, they should be reasonably understandable. If you would like more information, please check out the <a href="http://docs.jquery.com/Plugins/Validation/Methods">excellent documentation</a>.</p>
		
<div class="igBar"><span id="ljavascript-40"><a href="#" onclick="javascript:showPlainTxt('javascript-40'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-40">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">RegisterForm <span style="color: #339933;">=</span> $.<span style="color: #660066;">klass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; initialize <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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; username<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minlength<span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">4</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; email <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; password2 <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; equalTo<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#password1&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; messages <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; username <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Your username is required'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minlength<span style="color: #339933;">:</span> <span style="color: #3366CC;">'The minimum number of characters is 4'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; email <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; required<span style="color: #339933;">:</span> <span style="color: #3366CC;">'You must enter your email'</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email<span style="color: #339933;">:</span> <span style="color: #3366CC;">'You must enter a valid email'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; password2 <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; equalTo<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Your passwords must match'</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Notice that I am using <code>this.element</code> instead of <code>$('#lowpro-form')</code> inside the function. Low Pro provides this handy alias so you always work with the correct object. If you are going to be writing a complex function, it's always a good idea within the function to assign it to another variable.</p>
		
<p>But this code isn't actually doing anything yet. This is because we now need to bind the class to the form. Here is where we go back to the good old jQuery we know and love. Below the class, put this in the file:</p>

<div class="igBar"><span id="ljavascript-41"><a href="#" onclick="javascript:showPlainTxt('javascript-41'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-41">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lowpro-form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attach</span><span style="color: #009900;">&#40;</span>RegisterForm<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br /> 

<p>Now, open up the page in your browser and start to type into the fields. You should start to see the validation rules apply to the fields. Congratulations, you've written your first re-usable class with Low Pro!</p>
		
<h4>Enhancements</h4>
<p>Now that we have our validation rules, let's start to make the form a bit more sexy. First, something simple. Let's make sure that the first field (the username field) is always selected first, so the user can easily tab down the form. After the validation method inside the initalize function, add this:</p>

  <div class="igBar"><span id="ljavascript-42"><a href="#" onclick="javascript:showPlainTxt('javascript-42'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-42">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#username'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Refresh the page, and your username field should now get the focus. </p>
<p>Now let's introduce another event into the mix. When the user submits the form, we want to let the user know that it's being sent to the server. To do this, we could notify the user by changing the text on the button from "Register" to "Submitting".</p>
		
<div class="igBar"><span id="ljavascript-43"><a href="#" onclick="javascript:showPlainTxt('javascript-43'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-43">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">RegisterForm <span style="color: #339933;">=</span> $.<span style="color: #660066;">klass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; initialize <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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; ...</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; onsubmit <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $form <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">element</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.register'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=val"><span style="">val</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Submitting'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009966; font-style: italic;">/* If your using Ajax it goes in here */</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>So there we have it: A reusable, fully functional Ajax registration form with validation. You can <a href="http://examples.learningjquery.com/95/">check out a demo here</a> and also grab the source code of this example from my repository.</p>
		
<p>One final thing I should touch on in this first tutorial is the initialize function's options parameter. When attaching any Low Pro class, the initialize function accepts an object containing any options you want to pass into the function:</p>
		
<div class="igBar"><span id="ljavascript-44"><a href="#" onclick="javascript:showPlainTxt('javascript-44'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-44">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myform'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attach</span><span style="color: #009900;">&#40;</span>MyFunction<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>foo<span style="color: #339933;">:</span><span style="color: #3366CC;">'moo'</span><span style="color: #339933;">,</span> bar<span style="color: #339933;">:</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />
<p>In our above example, we'll add a tag to display a message of the day. In the HTML, create a div with a class of <code>motd</code>. Next, add the following code into the initalize function:</p>
		
<div class="igBar"><span id="ljavascript-45"><a href="#" onclick="javascript:showPlainTxt('javascript-45'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-45">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">motd</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.motd'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">motd</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>Now, when we attach our class to the form, we use the following:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lowpro-form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attach</span><span style="color: #009900;">&#40;</span>RegisterForm<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>motd<span style="color: #339933;">:</span> <span style="color: #3366CC;">'jQuery Rocks!'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>When you now refresh the form, you should see a message on the screen.</p>	
		
<p>Well, that wraps it up for this tutorial. I hope I've given you a small idea of the power that Low Pro contains. In conjunction with LiveQuery, you can really create some dynamic and interesting applications.</p>
		
<p>If anyone would be interested in more tutorials like this, then please leave a comment and let me know what you think.</p>
		
			<div class="vcard" id="hcard-Tane-Piper">
				<a rel="me" href="http://digitalspaghetti.me.uk/about" class="url fn">Tane Piper</a> <a href="mailto:digitalspaghetti@gmail.com" class="email" style="display: none;">digitalspaghetti@gmail.com</a> is a full time web developer, geek and <a href="http://www.humanism-scotland.org.uk/">Humanist</a> from <span class="adr geo"><a href="http://en.wikipedia.org/wiki/Edinburgh" class="locality">Edinburgh, Scotland</a>.<span style="display: none;">(<span class="latitude">55.957898</span>, <span class="longitude">-3.159792</span>)</span></span>
				He has released or is working on several open source projects including <a rel="me" href="http://jmaps.digitalspaghetti.me.uk">jMaps</a> and <a rel="me" href="http://hg.digitalspaghetti.me.uk/hgmanager">Mercurial manager</a>.
				He is also available <a rel="me" href="http://digitalspaghetti.me.uk/for-hire">for hire</a> as a freelance developer, working with PHP, Python and JavaScript.
			</div>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/05/using-low-pro-for-jquery/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Working with Events, part 1</title>
		<link>http://www.learningjquery.com/2008/03/working-with-events-part-1</link>
		<comments>http://www.learningjquery.com/2008/03/working-with-events-part-1#comments</comments>
		<pubDate>Mon, 31 Mar 2008 16:34:12 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2008/03/working-with-events-part-1</guid>
		<description><![CDATA[CSS and JavaScript are different in many ways, almost all of which are too obvious to mention. However, one difference between the two bears explanation, because it is often the cause of confusion and consternation, especially among those who are making the transition from CSS guru to jQuery novice. In fact, it was one of [...]]]></description>
			<content:encoded><![CDATA[<p>CSS and JavaScript are different in many ways, almost all of which are too obvious to mention. However, one difference between the two bears explanation, because it is often the cause of confusion and consternation, especially among those who are making the transition from CSS guru to jQuery novice. In fact, it was one of the first things I asked about on the jQuery mailing list back in 2006. Since then, I've seen at least one question on the subject every week, and sometimes as many as one per day&mdash;despite an <a href="http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_stop_working_after_an_Ajax_request.3F">FAQ page</a> and <a href="http://plugins.jquery.com/project/livequery">these</a> <a href="http://plugins.jquery.com/project/Listen">three</a> <a href="http://plugins.jquery.com/project/Intercept">plugins</a> to help users deal with it.
</p>
<h4>How CSS and JavaScript Are Different</h4>

<p>So, what's this important difference? </p>
<span id="more-92"></span>
<p><strong>In <abbr title="Cascading Style Sheets">CSS</abbr></strong>, style rules are automatically applied to any element that matches the selectors, <em>no matter when those elements are added to the document (DOM)</em>. </p>

<p><strong>In JavaScript</strong>, event handlers that are registered for elements in the document apply <em>only to those elements that are part of the DOM at the time the event is attached</em>. If we add similar elements to the DOM at a later time, whether through simple DOM manipulation or ajax, CSS will give those elements the same appearance, but JavaScript will not automatically make them act the same way. </p>

<p>For example, let's say we have "<code>&lt;button class="alert"&gt;Alert!&lt;/button&gt;</code>" in our document, and we want to attach a click handler to it that generates an alert message. In jQuery, we might do so with the following code:</p>

<div class="igBar"><span id="ljavascript-54"><a href="#" onclick="javascript:showPlainTxt('javascript-54'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-54">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.alert'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'this is an alert message'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Here we are registering the click handler for the button with a class of "alert" as soon as the DOM has loaded. So, the button is there, and we have a click function bound to it. If we add a second <code>&lt;button class="alert"&gt;</code> later on, however, it will know nothing about that click handler. The click event had been dealt with before this second button existed. So, the second button will not generate an alert.</p> 
[inline][/inline]
<p>Let's test what we've just discussed. I've added a script with the above three lines of jQuery code so that the following button will produce an alert message when clicked. Try it: <button class="alert">Alert!</button></p>

<h4>Events Don't Work with Added Elements</h4>
<p>Now, let's create a new button (if we don't already have a second one) using jQuery code like this:</p>

<div class="igBar"><span id="ljavascript-55"><a href="#" onclick="javascript:showPlainTxt('javascript-55'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-55">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#create-button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button.alert'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="">&lt;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;button class=&quot;alert&quot;&gt;Not another alert'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=insertAfter"><span style="">insertAfter</span></a><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p><a id="create-button" href="#">create the button</a></p>

<p>Have you clicked the link to create the second button? Great. Now click that button. It does nothing. Just as expected. </p>

<h4>CSS Continues to "Work" with Newly Created Elements</h4>
<p>Now let's take a look at another example. In this one, we have three list items&mdash;two plain items and one with a class of special:</p>

<div class="igBar"><span id="lhtml-56"><a href="#" onclick="javascript:showPlainTxt('html-56'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-56">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;list1&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;eventlist&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>plain<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;special&quot;</span><span style="color: #000000;">&gt;</span></span>special <span style="color: #009900;"><span style="color: #000000;">&lt;button&gt;</span></span>I am special<span style="color: #009900;"><span style="color: #000000;">&lt;</span><span style="color: #66cc66;">/</span>button&gt;&lt;<span style="color: #66cc66;">/</span>li&gt;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>plain<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Press the "I am special" button to create a <em>new</em> list item with a class of "special": </p>
<ul id="list1" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<p>Notice that, like the first special li, the new one has the yellow background. The CSS has come through for us. But press the newly created "I am new" button and, just as with the second alert above, nothing happens. The jQuery code we're using to add the new item says that upon clicking a button inside a list item with a class of "special" (which itself is inside an element with id of "list1") a new list item with class="special" should be inserted after the list item in which the button was clicked:</p>

<div class="igBar"><span id="ljavascript-57"><a href="#" onclick="javascript:showPlainTxt('javascript-57'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-57">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list1 li.special button'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>So, how can we get the events to carry over to the new elements? Two common approaches are <strong>event delegation</strong> and <strong>"re-binding" event handlers</strong>. In this entry, we'll examine event delegation; in part 2, we'll explore ways to re-bind.</p>

<h4>Event Delegation: Getting Events to Embrace New Elements</h4>
<p>The general idea of event delegation is to bind the event handler to a containing element and then have an action take place based on which specific element within that containing element is targeted. Let's say we have another unordered list: <code>&lt;ul id="list2"&gt; ... &lt;/ul&gt;</code>. Instead of attaching the <code>.click()</code> method to a button &mdash; <code>$('#list2 li.special button').click(...)</code> &mdash; we can attach it to the entire surrounding <code>&lt;ul&gt;</code>. Through the magic of "<a href="http://www.quirksmode.org/js/events_order.html">bubbling</a>," any click on the button is also a click on the button's surrounding list item, the list as a whole, the containing div, and all the way up to the window object. Since the <code>&lt;ul&gt;</code> that gets clicked is the same one each time (we're only creating items <em>within</em> the <code>&lt;ul&gt;</code>), the same thing will happen when clicking on all of the buttons, regardless of when they were created.</p>

<p>When we use event delegation, we need to pass in the "event" argument. So, in our case, instead of <code>.click()</code>, we'll have <code>.click(event)</code>. We don't have to name this argument <code>event</code>. We can call it <code>e</code> or <code>evt</code> or <code>gummy</code> or whatever we want. I just like to use labels that are as obvious as possible because I have a hard time keeping track of things. Here is what we have so far:</p>

<div class="igBar"><span id="ljavascript-58"><a href="#" onclick="javascript:showPlainTxt('javascript-58'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-58">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list2'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>So far, the code is very similar to our first attempt, except for the selector we're starting with (#list2) and the addition of the event argument. Now we need to determine whether what is being clicked inside the <code>&lt;ul&gt;</code> is a "special" button or not. If it is, we can add a new <code>&lt;li class="special"&gt;</code>. We check the clicked element by using the "target" property of the event argument:</p>

<div class="igBar"><span id="ljavascript-59"><a href="#" onclick="javascript:showPlainTxt('javascript-59'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-59">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#list2'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $newLi <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li class=&quot;special&quot;&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $tgt <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$tgt.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $tgt.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parent"><span style="">parent</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=after"><span style="">after</span></a><span style="color: #009900;">&#40;</span>$newLi<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// next 2 lines show that you've clicked on the ul</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> bgc <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'backgroundColor'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=css"><span style="">css</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>backgroundColor<span style="color: #339933;">:</span> bgc <span style="color: #339933;">==</span> <span style="color: #3366CC;">'#ffcccc'</span> <span style="color: #339933;">||</span> bgc <span style="color: #339933;">==</span> <span style="color: #3366CC;">'rgb(255, 204, 204)'</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'#ccccff'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'#ffcccc'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Line 4 above puts the target element in a jQuery wrapper and stores it in the $tgt variable. Line 5 checks whether the click's target is a button. If it is, the new list item is inserted after the parent of the clicked button. Let's try it:</p>

<ul id="list2" class="eventlist">
  <li>plain</li>
  <li class="special">special <button>I am special</button></li>
  <li>plain</li>
</ul>

<p>I put an additional two lines at the end to demonstrate that a click on one of the buttons is still considered a click on the <code>&lt;ul&gt;</code> You'll see that clicking anywhere within the <code>&lt;ul&gt;</code> toggles its background between pink and blue.</p>

<p>It's probably worth noting that jQuery makes working with the event argument cross-browser friendly. If you do this sort of thing with plain JavaScript and DOM nodes, you'd have to do something like this:</p>

<pre><code>var list2 = document.getElementById('list2');
list2.onclick = function(e) {
  var e = e || window.event;
  var tgt = e.target || e.srcElement;
  if (tgt.nodeName.toLowerCase() == 'button') {
    // do something
  }
};</code></pre>

<p>As you can see, it's a bit of a hassle.</p>

<h4>Another Huge Benefit of Event Delegation</h4>
<p>Event delegation is also a great way to avoid crippling the user's browser when you're working with a huge document. For example, if you have a table with thousands of cells, and you want something to happen when the user clicks on one, you won't want to attach a click handler to every single one of them (believe me, it can get ugly). Instead, you can attach the click handler to a single table element and use <code>event.target</code> to pinpoint the cell that is being clicked:</p>

<div class="igBar"><span id="ljavascript-60"><a href="#" onclick="javascript:showPlainTxt('javascript-60'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-60">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'table'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $thisCell<span style="color: #339933;">,</span> $tgt <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$tgt.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'td'</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $thisCell <span style="color: #339933;">=</span> $tgt;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$tgt.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parents"><span style="">parents</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'td'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $thisCell <span style="color: #339933;">=</span> $tgt.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=parents"><span style="">parents</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'td:first'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// now do something with $thisCell</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Note that I had to account for the possibility of clicking in a child/descendant of a table cell, but this seems a small inconvenience for the great performance increase that event delegation affords.</p>

<h4>Coming Up Next</h4>
<p>In <a href="http://www.learningjquery.com/2008/05/working-with-events-part-2">part 2</a> of this tutorial, we'll look at how to get events to carry over to newly created elements through careful placement of function calls. We'll also, necessarily, examine unbinding events and using namespaced events. In the meantime, I hope you find part 1 useful. If I've made any mistakes, especially in my terminology, please don't hesitate to point them.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/03/working-with-events-part-1/feed</wfw:commentRss>
		<slash:comments>93</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-66"><a href="#" onclick="javascript:showPlainTxt('html-66'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-66">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/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-67"><a href="#" onclick="javascript:showPlainTxt('javascript-67'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-67">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>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-68"><a href="#" onclick="javascript:showPlainTxt('javascript-68'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-68">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> 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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> widow <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">4</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startText <span style="color: #339933;">=</span> allText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endText <span style="color: #339933;">=</span> allText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>startText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>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-69"><a href="#" onclick="javascript:showPlainTxt('javascript-69'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-69">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> 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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> widow <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">4</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startText <span style="color: #339933;">=</span> allText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endText <span style="color: #339933;">=</span> allText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>startText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// *** commence DOM manipulation ... </span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> 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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; startText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;/span&gt;'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><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-70"><a href="#" onclick="javascript:showPlainTxt('javascript-70'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-70">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> 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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> widow <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">4</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dl.expander dd'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startText <span style="color: #339933;">=</span> allText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endText <span style="color: #339933;">=</span> allText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span>startText.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> 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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=html"><span style="">html</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; startText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endText<span style="color: #339933;">,</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'&lt;/span&gt;'</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #006600; font-style: italic;">// *** hide details until read-more link is clicked;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #006600; font-style: italic;">// then hide link and show details. </span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span.details'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=hide"><span style="">hide</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.read-more'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=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="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=fadeIn"><span style="">fadeIn</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>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>28</slash:comments>
		</item>
		<item>
		<title>Improved Animated Scrolling Script for Same-Page Links</title>
		<link>http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links</link>
		<comments>http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links#comments</comments>
		<pubDate>Sat, 20 Oct 2007 19:20:30 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links</guid>
		<description><![CDATA[After posting the last entry on animated scrolling with jQuery 1.2, I realized that I had left out an important piece of code. Actually, I didn't discover it until someone notified me that another page on the site was broken. Can you spot the problem(s)? [Note: the problem is not in line 3. The syntax [...]]]></description>
			<content:encoded><![CDATA[<p>After posting <a href="/2007/09/animated-scrolling-with-jquery-12">the last entry on animated scrolling with jQuery 1.2</a>, I realized that I had left out an important piece of code. Actually, I didn't discover it until someone notified me that another page on the site was broken. Can you spot the problem(s)? [Note: the problem is <em>not</em> in line 3. The syntax highlighter just can't handle the regular expression with two slashes in it ("//") and is incorrectly treating them as a comment mark.] See the answer below the code.</p>

<div class="igBar"><span id="ljavascript-79"><a href="#" onclick="javascript:showPlainTxt('javascript-79'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-79">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href*=#]'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">pathname</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pathname</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& location.<span style="color: #660066;">hostname</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $target <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> &#038;& $target</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[name='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">']'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> targetOffset <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">top</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html,body'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>scrollTop<span style="color: #339933;">:</span> targetOffset<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">1000</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<span id="more-78"></span>
<p><strong>Answer</strong>: The animated scrolling script hijacks links that look like this: <code>&lt;a href="#"&gt;</code>. A couple people confirmed in the comments that the script needed a bit more work, so I figured we could take one more pass at it. </p>

<p>By the way, even though we attached the click event handler to all links that have the "#" symbol <em>anywhere</em> in the <code>href</code>, the very next line ensures that the link is pointing to the same page &mdash; by checking for a match between <code>location.pathname</code> and <code>this.pathname</code> &mdash; and the line after that ensures that it's pointing to the same domain, by checking for a match between <code>location.hostname</code> and <code>this.hostname</code>. With this approach, we can accommodate same-page links whether they include a fully-qualified URL, a relative URL, or just the fragment identifier.</p>

<h4>Check for the Hash</h4>
<p>Let's fix the problem with the <code>&lt;a href="#"&gt;</code> links. The first thing we have to do is see if there is actually something following the "#" symbol in the <code>href</code>. Apparently, if there is a lone "#" symbol, without any following characters, Firefox and Internet Explorer don't consider it a hash. Safari does, however. So, to avoid a false positive on <code>&lt;a href="#"&gt;</code>, we need to first strip the "#" and then check if there is anything left. We can do so by adding this condition to the first <code>if</code> statement: <code>&amp;&amp; this.hash.replace(/#/,'')</code></p>

<h4>Check for the Named Anchor</h4>
<p>Since we're already changing the script, maybe it's a good time to make some of it more readable, too. This part with the "short-circuit" logic, using <code>&amp;&amp;</code> and <code>||</code>, makes me a little dizzy:</p>

<div class="igBar"><span id="ljavascript-80"><a href="#" onclick="javascript:showPlainTxt('javascript-80'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-80">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$target <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> &#038;& $target</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339933;">||</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[name='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">']'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
</ol></div>
</div></div><br />

<p>There is absolutely nothing wrong with this syntax. In fact, more advanced JavaScripters use it all the time. But I feel more comfortable using a simpler, more straightforward style. So, let's set two variables &mdash; one for a target ID and one for a target named anchor. We'll then use conditional (aka ternary) operators to set a third, <code>$target</code>, variable as the target ID if it's there, and if not, the target named anchor if it's there, and if not, <code>false</code>. Then we can just check if <code>$target</code> has some value (other than false):</p>

<div class="igBar"><span id="ljavascript-81"><a href="#" onclick="javascript:showPlainTxt('javascript-81'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-81">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> $targetId <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $targetAnchor <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[name='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $targetId.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">?</span> $targetId </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #339933;">:</span> $targetAnchor.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">?</span> $targetAnchor </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
</ol></div>
</div></div><br />

<p>Now it appears that the animated scrolling behavior will be attached to all same-page links and not break other stuff on the page. </p>

<h4>Loop First, Bind Last</h4>
<p>But there is another problem. Since we're still binding the <code>.click()</code> method to every link with "#" in it, even if it's appropriately avoiding applying the animation for some of those links, jQuery is still hijacking links that have an inline <code>onclick</code> handler (but, oddly, only the first time those links are clicked). To fix this problem, we can replace the <code>.click()</code> with <code>.each()</code>. Then we'll iterate through all links that have "#" somewhere in them, but place the conditions inside the loop so that we bind the click handler only after we've filtered out all the links that don't apply. Here is what the script looks like with the change:</p>

<div class="igBar"><span id="ljavascript-82"><a href="#" onclick="javascript:showPlainTxt('javascript-82'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-82">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href*=#]'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">pathname</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pathname</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& location.<span style="color: #660066;">hostname</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/#/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $targetId <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> $targetAnchor <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[name='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $targetId.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">?</span> $targetId <span style="color: #339933;">:</span> $targetAnchor.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">?</span> $targetAnchor <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> targetOffset <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">top</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html, body'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>scrollTop<span style="color: #339933;">:</span> targetOffset<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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Notice especially lines 2 and line 10. This change not only takes care of our problem, but it feels cleaner somehow, too. Is it more efficient? I don't know. Maybe someone else can tell us in the comments.</p>

<h4>Normalize Directory Indexes</h4>
<p>To be complete, we should probably take care of one more thing: the possibility that, on an "index" page, a link could point to "/path/index.htm" when the current location says "/path/" or vice versa. One way to "normalize" these index pages and links is to add a couple more <code>.replace()</code> methods to both sides of the equation in line 3.</p>

<h4>Update</h4>
<p>Aman suggested in a comment below that I make this process DRYer, and kangax provided a great example. So we can write a filter function and apply it to both sides rather than repeating the three replaces on each side:</p>

<div class="igBar"><span id="ljavascript-83"><a href="#" onclick="javascript:showPlainTxt('javascript-83'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-83">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">function</span> filterPath<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> string</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(index|default).[a-zA-Z]{3,4}$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #006600; font-style: italic;">// first additional replace </span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\/$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// second additional replace</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li>
</ol></div>
</div></div><br />

<p>The first additional <code>.replace()</code> will find a string represented  by "index" or "default," followed by a dot, followed by any three or four letters at the end the pathname, and replace it with an empty string (i.e. remove it). The second one will replace a trailing slash with an empty string. As with chained jQuery methods, these regular-expression methods can be placed on separate lines to improve readability. Finally, we have a bullet-proof (I hope) animated scrolling script for same-page links:</p>

<div class="igBar"><span id="ljavascript-84"><a href="#" onclick="javascript:showPlainTxt('javascript-84'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-84">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> filterPath<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> string</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> &nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(index|default).[a-zA-Z]{3,4}$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> &nbsp;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\/$/</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href*=#]'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> filterPath<span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> filterPath<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& location.<span style="color: #660066;">hostname</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/#/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $targetId <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> $targetAnchor <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[name='</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slice"><span style="">slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $targetId.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">?</span> $targetId <span style="color: #339933;">:</span> $targetAnchor.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a> <span style="color: #339933;">?</span> $targetAnchor <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> targetOffset <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">top</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html, body'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>scrollTop<span style="color: #339933;">:</span> targetOffset<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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>If you try it out, let me know how it goes.</p>

<h4><ins datetime="2008-01-29T14:32:13+00:00">Update 2</ins></h4>
<p>Ariel Flesler has written an <a href="http://plugins.jquery.com/project/scrollto">excellent ScrollTo plugin</a>, which he says was inspired by this blog entry. Be sure to check out <a href="http://www.freewebs.com/flesler/jQuery.ScrollTo/">the demo</a>.</p>

<h4 id="update3"><ins datetime="2008-09-09T03:26:51+00:00">Update 3</ins></h4>
<p>Someone called my attention to a problem that this script was having in IE and Opera. Not sure how I could have missed that, because I'm sure I tested it in both of those browsers. But never mind, I've come up with a little patch:</p>

<div class="igBar"><span id="ljavascript-85"><a href="#" onclick="javascript:showPlainTxt('javascript-85'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-85">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> filterPath<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> string</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(index|default).[a-zA-Z]{3,4}$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\/$/</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> locationPath <span style="color: #339933;">=</span> filterPath<span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href*=#]'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> thisPath <span style="color: #339933;">=</span> filterPath<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> locationPath;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> &nbsp;locationPath <span style="color: #339933;">==</span> thisPath</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& <span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">hostname</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/#/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> target <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> targetOffset <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">top</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html, body'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>scrollTop<span style="color: #339933;">:</span> targetOffset<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">400</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> target;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Apparently, IE doesn't see a <code>hostname</code> or <code>pathname</code> if a link's <code>href</code> attribute is set with JavaScript and contains only a hash (such as "#example"). So, I'm checking now for either a match or an absence of <code>hostname</code> and <code>pathname</code>.</p>

<p>I hope this change fixes the problem that <a href="/2007/09/animated-scrolling-with-jquery-12#comment-54939">Mike</a> was having. Seems to work in my tests now. Oh, and I took the opportunity to improve the code a bit. Now, it has mild back-button support: while clicking on the back button doesn't produce the animated scrolling, it at least gets you back to the previous location.</p>

<h4 id="update4"><ins datetime="2009-01-16T04:11:27+00:00">Update 4</ins></h4>
<p>Ariel Flesler suggested that the problem a few people have mentioned regarding this script with Opera has to do with this line: <code> $('html, body').animate({scrollTop: targetOffset}, 400);</code>. At the time I wrote the script, I was covering all my bases, since "html" worked for IE in standards mode and "body" worked in quirks mode. Apparently, though, the combination of the two messes things up in Opera. So, try removing "body" from the selector and see if that fixes things.</p>
<h4 id="update5"><ins datetime="2009-12-29T04:11:55+00:00">Update 5</ins></h4>
<p>Here is a fix for the problem people have with Opera (and possibly other browsers) when attaching the animate to both html and body:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> filterPath<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> string</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\//</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(index|default).[a-zA-Z]{3,4}$/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\/$/</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> locationPath <span style="color: #339933;">=</span> filterPath<span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> scrollElem <span style="color: #339933;">=</span> scrollableElement<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'html'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href*=#]'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> thisPath <span style="color: #339933;">=</span> filterPath<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> locationPath;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> &nbsp;locationPath <span style="color: #339933;">==</span> thisPath</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& <span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">hostname</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &#038;& <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/#/</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $target <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> target <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hash</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> targetOffset <span style="color: #339933;">=</span> $target.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=offset"><span style="">offset</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">top</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>scrollElem<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=animate"><span style="">animate</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>scrollTop<span style="color: #339933;">:</span> targetOffset<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">400</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> target;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #006600; font-style: italic;">// use the first element that is &quot;scrollable&quot;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #003366; font-weight: bold;">function</span> scrollableElement<span style="color: #009900;">&#40;</span>els<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">0</span><span style="color: #339933;">,</span> argLength <span style="color: #339933;">=</span> arguments.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=length"><span style="">length</span></a>; i <span style="color: #339933;">&lt;</span>argLength; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #339933;">=</span> arguments<span style="color: #009900;">&#91;</span>i<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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scrollElement <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$scrollElement.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="">&gt;</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> el;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $scrollElement.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> isScrollable <span style="color: #339933;">=</span> $scrollElement.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="">&gt;</span> <span style="color: #CC0000;color:#800000;">0</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $scrollElement.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isScrollable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> el;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>I put together a little <a href="http://github.com/kswedberg/jquery-smooth-scroll">smooth-scroll jQuery plugin</a> with a couple options, in case anyone is interested. For a more robust feature set, check out Ariel Flesler's <a href="http://flesler.blogspot.com/2007/10/jqueryscrollto.html">scrollTo plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links/feed</wfw:commentRss>
		<slash:comments>80</slash:comments>
		</item>
		<item>
		<title>Automatic Page Contents</title>
		<link>http://www.learningjquery.com/2007/06/automatic-page-contents</link>
		<comments>http://www.learningjquery.com/2007/06/automatic-page-contents#comments</comments>
		<pubDate>Mon, 25 Jun 2007 11:44:05 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2007/06/automatic-page-contents</guid>
		<description><![CDATA[It's been so long since I last posted a tutorial here that I'm afraid everyone might have forgotten about the place. For the past few months, there has been a little "Page Contents" menu at the top-right corner of some of the pages on this site &#8212; actually, any page that has more than one [...]]]></description>
			<content:encoded><![CDATA[<p>It's been so long since I last posted a tutorial here that I'm afraid everyone might have forgotten about the place. For the past few months, there has been a little "Page Contents" menu at the top-right corner of some of the pages on this site &mdash; actually, any page that has more than one <code>&lt;h2&gt;</code> elements in the main content area. In this entry, I'd like to demonstrate how to create an automatic page contents list using jQuery.</p>
<span id="more-44"></span>
<h4>The Plugin Option</h4>
<p>Before we begin, I should let you know that there are a number of plugins that do what I'm about to show you. If you want to go the plug-in route, take a look at one of these:</p>
<ul>
  <li><a href="http://nepherim.lifehaiku.com/2007/jqtoc">jqTOC</a> by Dave G.</li>
  <li><a href="http://users.tpg.com.au/j_birch/plugins/jquery.contentMenu.js">Page Contents Menu</a> (.js file) by Joel Birch</li>
</ul>
<p>But if you want to see how to do this sort of thing on your own, read on...</p>
<h4>Let's Begin</h4>
<p>Here is what we need to do:</p>
<ol>
  <li>Find out if there is more than one <code>&lt;h2&gt;</code> in the main content area of the page (on this site, the home page and most listing pages will have more than one). If so, we start building the table of contents...</li>
  <li>Create a new container <code>&lt;div&gt;</code> for the table of contents and insert a heading and an empty div inside.</li>
  <li>Copy the <code>&lt;h2&gt;</code> elements and the <code>&lt;a&gt;</code> elements inside them.</li>
  <li>Change the attributes of the copied links.</li>
  <li>Insert each copied <code>&lt;h2&gt;</code> along with its contained link into the page-contents <code>&lt;div&gt;</code>.</li>
</ol>
<img class="alignright" src="/images/page-contents.png" alt="page contents example" />
<p>When we're finished, we should have something that looks like the image to the right:</p>

<h4>Create the container</h4>
<p>Okay, so our first step is to create the container. We'll wrap our code in a <code>$(document).ready()</code>, as we do nearly every time, so that the script loads when the DOM (but not necessarily image, etc.) has loaded. Then we make sure that we're dealing with more than one <code>&lt;h2&gt;</code> and, if so, start building:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h2'</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;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;page-contents&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=prepend"><span style="">prepend</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;h3&gt;Page Contents&lt;/h3&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=prependTo"><span style="">prependTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Okay, maybe a little explanation is warranted here. The first line inside our <code>if</code> condition (line 3) creates a new <code>&lt;div&gt;</code> element with an <code>id</code> of "page-contents." Line 4 inserts an <code>&lt;h3&gt;</code> at the beginning of that new <code>&lt;div&gt;</code> and line 5 inserts an empty <code>&lt;div&gt;</code> at the end of the page-contents <code>&lt;div&gt;</code>. Finally, line 5 inserts the whole group of newly created elements at the beginning of the <code>&lt;body&gt;</code>.</p>
<h4>Copy the headings</h4>
<p>The next part involves copying all of the <code>&lt;h2&gt;</code> elements, and while we're at it, changing the <code>href</code> of the copied <code>&lt;a&gt;</code> elements to point to the original <code>&lt;h2&gt;</code>s. How do we know where to point them? Each of the original <code>&lt;h2&gt;</code>s has an <code>id</code> associated with it. We just take the <code>id</code>, prepend a "#", and attach it to the <code>href</code>:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h2'</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;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ... etc. ...</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h2'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> thisId <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $this</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=clone"><span style="">clone</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'title'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'jump to '</span> <span style="color: #339933;">+</span> $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'href'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> thisId</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'pc-'</span> <span style="color: #339933;">+</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=index"><span style="">index</span></a><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#page-contents div'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; &nbsp; </div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>We use a <code>.each()</code> method here so that we can apply a number of things to each <code>&lt;h2&gt;</code> in turn. Inside this method, we start by declaring a couple variables&mdash;one for <code>$(this)</code>, because we use it twice, and one for each <code>id</code>, because we use it to set the <code>href</code>s. </p>
  
<p>So, for each <code>&lt;h2&gt;</code>, we make a copy using <code>.clone()</code> and find the <code>&lt;a&gt;</code> inside of it. Then we set each link's <code>title</code> and <code>href</code> attribute.</p>

<p>When we're finished with the links, we back up to the cloned <code>&lt;h2&gt;</code> elements by using <code>.end()</code>. We want to ensure that we don't duplicate the <code>id</code> along with the element, so we prepend "pc-" to it, keeping each one unique.</p>

<p>When all is ready, we append each <code>&lt;h2&gt;</code> to the <code>&lt;div&gt;</code>inside the page-contents container.</p>

<h4>A Little Extra Flair</h4>
<p>Now, for an optional addition to our script, we can hide the page-contents <code>&lt;div&gt;</code> in a style sheet (by setting it to <code>display: none</code>). Then we attach a click handler to the page-contents heading with a <code>.toggleClass()</code> to change the background image and a <code>.slideToggle()</code> to show and hide the same-page links. </p>

<p>Here is the complete script, with the additional click handler at the end:</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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=ready"><span style="">ready</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h2'</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;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;page-contents&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=prepend"><span style="">prepend</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;h3&gt;Page Contents&lt;/h3&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=append"><span style="">append</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=prependTo"><span style="">prependTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>; &nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h2'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=each"><span style="">each</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> $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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> thisId <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $this</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=clone"><span style="">clone</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=find"><span style="">find</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'title'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'jump to '</span> <span style="color: #339933;">+</span> $this.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'href'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> thisId</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=attr"><span style="">attr</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'pc-'</span> <span style="color: #339933;">+</span> <a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=index"><span style="">index</span></a><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#page-contents div'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#page-contents h3'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=click"><span style="">click</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=toggleClass"><span style="">toggleClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'arrow-down'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=next"><span style="">next</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=slideToggle"><span style="">slideToggle</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>If you want to see it in action, just go to the <a href="http://www.learningjquery.com/">Learning jQuery home page</a> and look in the top-right corner of the page.</p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2007/06/automatic-page-contents/feed</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Copy Events from One Element to Another</title>
		<link>http://www.learningjquery.com/2007/01/copy-events-from-one-element-to-another</link>
		<comments>http://www.learningjquery.com/2007/01/copy-events-from-one-element-to-another#comments</comments>
		<pubDate>Thu, 01 Feb 2007 01:00:06 +0000</pubDate>
		<dc:creator>Brandon Aaron</dc:creator>
				<category><![CDATA[DOM Modification]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[jQuery Resources]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/2007/01/copy-events-from-one-element-to-another</guid>
		<description><![CDATA[Need to clone an element and its events? Sure, you could rebind the events after doing the clone, but that wouldn't be very DRY now, would it? Introducing Copy Events, a new plugin for jQuery.

Clone an Element and its Events

The Copy Event plugin makes it easy to clone an element and its events. How? Amazingly [...]]]></description>
			<content:encoded><![CDATA[<p>Need to clone an element and its events? Sure, you could rebind the events after doing the clone, but that wouldn't be very <abbr title="Don't Repeat Yourself">DRY</abbr> now, would it? Introducing <em>Copy Events</em>, a new plugin for jQuery.</p>
<span id="more-51"></span>
<h4>Clone an Element and its Events</h4>

<p>The Copy Event plugin makes it easy to clone an element <em>and</em> its events. How? Amazingly simple. Let's say you have an unordered list and each list item has a click, mouseover, and mouseout event. Now you need to clone the last list item and append it to the unordered list.</p>

<p>Here is the HTML:</p>

<div class="igBar"><span id="lhtml-99"><a href="#" onclick="javascript:showPlainTxt('html-99'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-99">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myList&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>List Item 1<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>List Item 2<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>List Item 3<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Here is the JavaScript that applies the event handlers during the <code>$(document).ready</code> event:</p>

<div class="igBar"><span id="ljavascript-100"><a href="#" onclick="javascript:showPlainTxt('javascript-100'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-100">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList li'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</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: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=addClass"><span style="">addClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'over'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=bind"><span style="">bind</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=removeClass"><span style="">removeClass</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'over'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Now, here is the code to clone the last list item with its events:</p>

<div class="igBar"><span id="ljavascript-101"><a href="#" onclick="javascript:showPlainTxt('javascript-101'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-101">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList li:last'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<span style="color: #660066;">cloneWithEvents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=text"><span style="">text</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'List Item '</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList li'</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=size"><span style="">size</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>DONE! Simple as calling <code>cloneWithEvents()</code>. This method takes an optional parameter called "deep" &mdash; just like the <code>clone()</code> method &mdash; which, if set to <code>false</code>, copies only the element itself, without any of its descendants. You can see the results <a href="http://examples.learningjquery.com/51/cloneWithEvents.html">here</a>.</p>

<h4>More Than Just Cloning</h4>
<p>The Copy Events plugin offers two more methods to allow for easy copying of events from one element to another or to several other elements. The first is the <code>copyEvents()</code> method and copies events from an element to the elements in the jQuery object. The second is the <code>copyEventsTo()</code> method which copies events from the first matched element in the jQuery object to the matched elements passed in. Let's take a closer look at both of these methods using the unordered list as an example again.</p>

<h4>The <code>copyEvents()</code> Method</h4>
<p>The <code>copyEvents()</code> method will copy all event handlers from a given element to the set of elements in the current jQuery object. What does that mean? Using the unordered list example, let's add some list items to the unordered list using AJAX.</p>

<p>Here is the code for the AJAX call that gets the extra list items, copies the events and adds the items to the list:</p>

<div class="igBar"><span id="ljavascript-102"><a href="#" onclick="javascript:showPlainTxt('javascript-102'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-102">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=$.get"><span style="">$.<span style="color: #660066;">get</span></span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'list-items.html'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>lis<span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">copyEvents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList li'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=appendTo"><span style="">appendTo</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Notice I passed a selector to the <code>copyEvents()</code> method. This method can take a jQuery object, a DOM Element and/or a selector string. When passing in a jQuery object or selector, it will use the first element to copy events from.</p>

<p>You can see the results for this <code>copyEvents</code> example <a href="http://examples.learningjquery.com/51/copyEvents.html">here</a>.</p>

<h4>The <code>copyEventsTo()</code> Method</h4>
<p>The <code>copyEventsTo()</code> method acts similar to the <code>appendTo()</code> method. It copies events from the first element in the current jQuery object to the passed in elements. Just like the <code>copyEvents()</code> method, the <code>copyEventsTo()</code> method can also take a jQuery object, a DOM Element and/or a selector string. </p>

<p>Let's say we have a new unordered list and would like to copy the events from the existing list to the new list when clicking on a special link:</p>

<div class="igBar"><span id="lhtml-103"><a href="#" onclick="javascript:showPlainTxt('html-103'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-103">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;copier&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Copy Events&quot;</span><span style="color: #000000;">&gt;</span></span>Copy Events<span style="color: #009900;"><span style="color: #000000;">&lt;/a&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;newList&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>New List Item 1<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>New List Item 2<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;li&gt;</span></span>New List Item 3<span style="color: #009900;"><span style="color: #000000;">&lt;/li&gt;</span></span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;"><span style="color: #000000;">&lt;/ul&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Now let's look at the JavaScript to copy these events over to the new list:</p>

<div class="igBar"><span id="ljavascript-104"><a href="#" onclick="javascript:showPlainTxt('javascript-104'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-104">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#copier'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; .<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=one"><span style="">one</span></a><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList li'</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">copyEventsTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#newList li'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<a href="http://www.learningjquery.com/wp-content/themes/ljq/docs.php?fn=remove"><span style="">remove</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: bold; vertical-align:top;color:#ACAA9A;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #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="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>I've used the <code>one</code> method to bind the click only once to the <code>&lt;a&gt;</code> tag. When clicked, the events will be copied from the first list item in the #myList unordered list.</p>

<p>Check out the example for yourself <a href="http://examples.learningjquery.com/51/copyEventsTo.html">here</a>.</p>

<h4>Next Steps</h4>
<p>The Copy Events plugin can be found in the <a href="http://docs.jquery.com/Downloading_jQuery#Subversion_.28SVN.29">jQuery Plugins SVN repository</a>. </p>
<div class="update">
<h4>Update</h4>
<p>The ability to copy events has been added to jQuery core as of jQuery 1.2.x. Simply use <code>.clone(true)</code>.</p>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2007/01/copy-events-from-one-element-to-another/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
