<?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>AndySowards.com :: Daily Professional Web Design, Development, Programming Freelancer, Hacks, Downloads, Math and being a Web 2.0 Hipster? &#187; Javascript</title>
	<atom:link href="http://www.andysowards.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andysowards.com/blog</link>
	<description>Professional Web Design, Freelancer, Tech, Development, Programming, Gadgets, Life, Hacks, Downloads, Math and being a Web 2.0 Hipster?</description>
	<lastBuildDate>Sat, 31 Jul 2010 01:02:43 +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>jQuery 1.3 with PHP &#8211; Free Chapter Download!</title>
		<link>http://www.andysowards.com/blog/programming/jquery-1-3-with-php-free-chapter-download/</link>
		<comments>http://www.andysowards.com/blog/programming/jquery-1-3-with-php-free-chapter-download/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 05:19:28 +0000</pubDate>
		<dc:creator>Andy Sowards</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Reviews]]></category>
		<category><![CDATA[freebies]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[popular]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[1.3]]></category>
		<category><![CDATA[1.4]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[chapter]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[freebie]]></category>
		<category><![CDATA[packt]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[preview]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://www.andysowards.com/blog/?p=2546</guid>
		<description><![CDATA[
As some of you know I have recently been read Learning jQuery 1.3 Book from Packt Publishing and will be posting an in depth review of the content from start to finish very soon, so stay tuned!
I realize that jQuery 1.4 is already out and the Buzz around it is pretty high &#8211; however, if [...]<script type="text/javascript">
Vertical1238300 = false;
ShowAdHereBanner1238300 = true;
RepeatAll1238300 = false;
NoFollowAll1238300 = false;
BannerStyles1238300 = new Array(
    "a{display:block;font-size:11px;color:#888;font-family:verdana,sans-serif;margin:0 4px 10px 0;text-align:center;text-decoration:none;overflow:hidden;}",
    "img{border:0;clear:right;}",
    "a.adhere{color:#666;font-weight:bold;font-size:12px;border:1px solid #ccc;background:#e7e7e7;text-align:center;}",
    "a.adhere:hover{border:1px solid #999;background:#ddd;color:#333;}"
);
document.write(unescape("%3Cscript src='"+document.location.protocol+"//s3.buysellads.com/1238300/1238300.js?v="+Date.parse(new Date())+"' type='text/javascript'%3E%3C/script%3E"));
</script>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.packtpub.com/jquery-1-3-with-php/mid/121109cltpgi?utm_source=andysowards.com&#038;utm_medium=affiliate&#038;utm_content=blog&#038;utm_campaign=mdb_001439"><img src="http://www.andysowards.com/blog/wp-content/uploads/2010/01/jquery-with-php.jpg" alt="P7290071-540-jquery-1.3-book-post" class="aligncenter size-full wp-image-1578" /></a></p>
<p>As some of you know I have recently been read <a href="http://www.packtpub.com/learning-jquery-1.3/mid/240609o1fp7w?utm_source=andysowards.com%2Fblog&#038;utm_medium=affiliate&#038;utm_content=blog&#038;utm_campaign=mdb_000083" target="_blank">Learning jQuery 1.3 Book from Packt Publishing</a> and will be posting an in depth review of the content from start to finish very soon, so stay tuned!</p>
<p>I realize that jQuery 1.4 is already out and the Buzz around it is pretty high &#8211; however, if you are new to jQuery &#8211; You shouldn&#8217;t worry about jumping into the latest and greatest version. You should instead focus on learning the basic concepts of jQuery, and using them correctly, and once you are up to speed, then by all means jump into 1.4. but learning the foundation of jQuery from the ground up will save you a lot of time and frustration!</p>
<p>In the meantime, I have also been reading <a href="http://www.packtpub.com/jquery-1-3-with-php/mid/121109cltpgi?utm_source=andysowards.com&#038;utm_medium=affiliate&#038;utm_content=blog&#038;utm_campaign=mdb_001439" target="_blank">jQuery 1.3 with PHP</a> and will be posting a review to that book as well very soon. If you can&#8217;t wait for the review then check out “Chapter 9: Data Tables” Yourself!! </p>
<p>You can get your hands on the free Chapter 9 download <a href="http://www.packtpub.com/files/6989-php-and-jquery-sample-chapter-9-data-tables.pdf" target="_blank">here</a>.</p>
<p>From what I have read of &#8220;jQuery 1.3 with PHP&#8221; it is an Excellent resource for those who want to go all out and create a very nice web application on your own. This is a great read for those looking to &#8216;tie it all together&#8217; and build a nice web app quickly and easily.</p>
<p>Its definitely <a href="http://www.packtpub.com/jquery-1-3-with-php/mid/121109cltpgi?utm_source=andysowards.com&#038;utm_medium=affiliate&#038;utm_content=blog&#038;utm_campaign=mdb_001439" target="_blank">worth picking up</a> because you can learn a lot of good techniques for more efficient and clean code!</p>
<p>Stay tuned and <a href="http://www.andysowards.com/blog/feed" target="_blank">Subscribe to the feed</a> so that you won&#8217;t miss the awesome FULL review of the book!<br />
<h3 class='related_post_title'>If you like this, You&#8217;ll love These.</h3>
<ul class='related_post'>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-26/' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/08/2008'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links For 10/08/2008' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/08/2008' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-26/' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/08/2008'>AndySowards.com :: Web Development Nerdy Daily Links For 10/08/2008</a></span></li>
<li><a href='http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/10/packt-jquery-part3-thumb.jpg' alt='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial' /> </a><span><a href='http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial'>How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-145/' title='AndySowards.com :: Web Development Nerdy Daily Links For 2/20/2009'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links For 2/20/2009' title='AndySowards.com :: Web Development Nerdy Daily Links For 2/20/2009' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-145/' title='AndySowards.com :: Web Development Nerdy Daily Links For 2/20/2009'>AndySowards.com :: Web Development Nerdy Daily Links For 2/20/2009</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-38/' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-38/' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008'>AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-13/' title='#WDNDL For 01/14/2010 – Tech News, jQuery Tips, Photoshop Tutorials, &amp; Inspirations!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 01/14/2010 – Tech News, jQuery Tips, Photoshop Tutorials, &amp; Inspirations!' title='#WDNDL For 01/14/2010 – Tech News, jQuery Tips, Photoshop Tutorials, &amp; Inspirations!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-13/' title='#WDNDL For 01/14/2010 – Tech News, jQuery Tips, Photoshop Tutorials, &amp; Inspirations!'>#WDNDL For 01/14/2010 – Tech News, jQuery Tips, Photoshop Tutorials, &#038; Inspirations!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-08-04/' title='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! '><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! ' title='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! ' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-08-04/' title='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! '>#WDNDL For 8/5/2009 – Design Inspiration &#038; Tips, Photoshop Freebies, &#038; jQuery Plugins! </a></span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andysowards.com/blog/programming/jquery-1-3-with-php-free-chapter-download/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial</title>
		<link>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/</link>
		<comments>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 23:02:38 +0000</pubDate>
		<dc:creator>Packt Publishing</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[popular]]></category>
		<category><![CDATA[1.3]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[adding]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[elements]]></category>
		<category><![CDATA[flair]]></category>
		<category><![CDATA[flare]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jquery1.3]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[packt]]></category>
		<category><![CDATA[part]]></category>
		<category><![CDATA[part3]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[series]]></category>
		<category><![CDATA[teach]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[with]]></category>
		<category><![CDATA[working]]></category>

		<guid isPermaLink="false">http://www.andysowards.com/blog/?p=2095</guid>
		<description><![CDATA[Welcome back to part 3 of Adding Flair to your Actions with jQuery! If you missed Part 2, go ahead and read that first, or just move ahead!
Simultaneous versus queued effects 
The .animate()  method, as we&#8217;ve just discovered, is very useful for creating simultaneous effects in a particular set of elements. There may be [...]<script type="text/javascript">
Vertical1238300 = false;
ShowAdHereBanner1238300 = true;
RepeatAll1238300 = false;
NoFollowAll1238300 = false;
BannerStyles1238300 = new Array(
    "a{display:block;font-size:11px;color:#888;font-family:verdana,sans-serif;margin:0 4px 10px 0;text-align:center;text-decoration:none;overflow:hidden;}",
    "img{border:0;clear:right;}",
    "a.adhere{color:#666;font-weight:bold;font-size:12px;border:1px solid #ccc;background:#e7e7e7;text-align:center;}",
    "a.adhere:hover{border:1px solid #999;background:#ddd;color:#333;}"
);
document.write(unescape("%3Cscript src='"+document.location.protocol+"//s3.buysellads.com/1238300/1238300.js?v="+Date.parse(new Date())+"' type='text/javascript'%3E%3C/script%3E"));
</script>]]></description>
			<content:encoded><![CDATA[<p>Welcome back to part 3 of Adding Flair to your Actions with jQuery! If you missed <a href="http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial/">Part 2</a>, go ahead and read that first, or just move ahead!</p>
<h2>Simultaneous versus queued effects </h2>
<p>The <i>.animate() </i> method, as we&#8217;ve just discovered, is very useful for creating simultaneous effects in a particular set of elements. There may be times, however, when we want to <b>queue </b> our effects, having them occur one after the other. </p>
<h2>Working with a single set of elements </h2>
<p>When applying multiple effects to the same set of elements, <b>queuing </b> is easily achieved by chaining those effects. To demonstrate this queuing, we&#8217;ll again move the <b>Text Size </b> box to the right, increase its height and increase its border width. This time, however, we perform the three effects sequentially, simply by placing each in its own <i>.animate() </i> method and chaining the three together: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('div.label').click(function() { <br/>    var paraWidth = $('div.speech p').outerWidth(); <br/>    var $switcher = $(this).parent(); <br/>    var switcherWidth = $switcher.outerWidth(); <br/>    $switcher <br/> <span style="font-weight: bold;">.animate({left: paraWidth - switcherWidth}, </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">                                            'slow') </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      .animate({height: '+=20px'}, 'slow') </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      .animate({borderWidth: '5px'}, 'slow'); </span> <br/>  }); <br/>}); <br/> </pre>
<p>Chaining permits us to keep all three <i>.animate() </i> methods on the same line, but here we have indented them and put each on its own line for greater readability. </p>
<p>We can queue any of the jQuery effect methods, not just <i>.animate() </i>, by chaining them. We can, for example, queue effects on in the following order: </p>
<ol>
<li>Fade its opacity to .5 with <i>.fadeTo() </i>. </li>
<li>Move it to the right with <i>.animate() </i>. </li>
<li>Fade it back in to full opacity with <i>.fadeTo() </i>. </li>
<li>Hide it with <i>.slideUp() </i>. </li>
<li>Show it once more with <i>.slideDown() </i>. </li>
</ol>
<p>All we need to do is chain the effects in the same order in our code: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>    $('div.label').click(function() { <br/>      var paraWidth = $('div.speech p').outerWidth(); <br/>      var $switcher = $(this).parent(); <br/>      var switcherWidth = $switcher.outerWidth(); <br/>      $switcher <br/> <span style="font-weight: bold;">.fadeTo('fast',0.5) </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        .animate({ </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">          'left': paraWidth - switcherWidth </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        }, 'slow') </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        .fadeTo('slow',1.0) </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        .slideUp('slow') </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        .slideDown('slow'); </span> <br/>    }); <br/>}); <br/> </pre>
<p>But what if we want to move the to the right at the same time as it fades to half opacity? If the two animations were occurring at the same speed, we could simply combine them into a single <i>.animate() </i> method. But in this example, the fade is using the <i>&#8216;fast&#8217; </i> speed while the move to the right is using the <i>&#8217;slow&#8217; </i> speed. Here is where the second form of the <i>.animate() </i> method comes in handy: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>    $('div.label').click(function() { <br/>      var paraWidth = $('div.speech p').outerWidth(); <br/>      var $switcher = $(this).parent(); <br/>      var switcherWidth = $switcher.outerWidth(); <br/>      $switcher <br/>        .fadeTo('fast',0.5) <br/> <span style="font-weight: bold;">.animate({ </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">          'left': paraWidth - switcherWidth </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        }, {duration: 'slow', queue: false}) </span> <br/>        .fadeTo('slow',1.0) <br/>        .slideUp('slow') <br/>        .slideDown('slow'); <br/>    }); <br/>}); <br/> </pre>
<p>The second argument, an options map, provides the <i>queue </i> option, which when set to <i>false </i> makes the animation start simultaneously with the previous one. </p>
<p>One final observation about queuing effects on a single set of elements is that queuing does not automatically apply to other, non-effect methods such as <i>.css() </i>. So let&#8217;s suppose we wanted to change the background color of to red after the <i>.slideUp() </i> but before the <i>slideDown() </i>. We could try doing it like this: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>$('div.label').click(function() { <br/>      var paraWidth = $('div.speech p').outerWidth(); <br/>      var $switcher = $(this).parent(); <br/>      var switcherWidth = $switcher.outerWidth(); <br/>      $switcher <br/>        .fadeTo('fast',0.5) <br/>        .animate({ <br/>          'left' paraWidth - switcherWidth <br/>        }, 'slow') <br/>        .fadeTo('slow',1.0) <br/>        .slideUp('slow') <br/> <span style="font-weight: bold;">.css('backgroundColor','#f00') </span> <br/>        .slideDown('slow'); <br/>      }); <br/>}); <br/> </pre>
<p>However, even though the background-changing code is placed at the correct position in the chain, it occurs immediately upon the click. </p>
<p>One way we can add non-effect methods to the queue is to use the appropriately named <i>.queue() </i> method. Here is what it would look like in our example: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>    $('div.label').click(function() { <br/>      var paraWidth = $('div.speech p').outerWidth(); <br/>      var $switcher = $(this).parent(); <br/>      var switcherWidth = $switcher.outerWidth(); <br/>      $switcher <br/>        .fadeTo('fast',0.5) <br/>        .animate({ <br/>          'left': paraWidth - switcherWidth <br/>        }, 'slow') <br/>        .fadeTo('slow',1.0) <br/>        .slideUp('slow') <br/> <span style="font-weight: bold;">.queue(function() { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">          $switcher </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">            .css('backgroundColor', '#f00') </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">            .dequeue(); </span> <br/>        }) <br/>        .slideDown('slow'); <br/>    }); <br/>}); <br/> </pre>
<p>When given a <b>callback function </b>, as it is here, the <i>.queue() </i> method adds the function to the queue of effects for the matched elements. Within the function, we set the background color to red and then add the corollary <i>.dequeue() </i> method. Including this <i>.dequeue() </i> method allows the animation queue to pick up where it left off and complete the chain with the following <i>.slideDown(&#8217;slow&#8217;) </i> line. If we hadn&#8217;t used .dequeue(), the animation would have stopped. </p>
<p style="margin-left: 20px; margin-right: 20px;"> <em>More information and examples for .queue() and .dequeue() are available at <a href="http://docs.jquery.com/Effects" target="_blank">http://docs.jquery.com/Effects </a>. </em> </p>
<p>We&#8217;ll discover another way to queue non-effect methods as we examine effects with multiple sets of elements. </p>
<h2>Working with multiple sets of elements </h2>
<p>Unlike with a single set of elements, when we apply effects to different sets, they occur at virtually the same time. To see these simultaneous effects in action, we&#8217;ll slide one paragraph down while sliding another paragraph up. First, we&#8217;ll add the remaining portion of the Gettysburg Address to the HTML, dividing it into two separate paragraphs: </p>
<pre style="margin-left: 20px;">
<div id="switcher"> <br/>
<div class="label">Text Size </div>

 <br/> <button id="switcher-default">Default </button> <br/> <button id="switcher-large">Bigger </button> <br/> <button id="switcher-small">Smaller </button> <br/> </div>

 <br/>
<div class="speech"> <br/>

Fourscore and seven years ago our fathers brought forth <br/>     on this continent a new nation, conceived in liberty, and <br/>     dedicated to the proposition that all men are created <br/>     equal. <br/> 

 <br/>

Now we are engaged in a great civil war, testing whether <br/>     that nation, or any nation so conceived and so dedicated, <br/>     can long endure. We are met on a great battlefield of <br/>     that war. We have come to dedicate a portion of that <br/>     field as a final resting-place for those who here gave <br/>     their lives that the nation might live. It is altogether <br/>     fitting and proper that we should do this. But, in a <br/>     larger sense, we cannot dedicate, we cannot consecrate, <br/>     we cannot hallow, this ground. <br/> 

 <br/> <a href="#" class="more">read more </a> <br/> <span style="font-weight: bold;">

The brave men, living and dead, who struggled </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     here have consecrated it, far above our poor </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     power to add or detract. The world will little </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     note, nor long remember, what we say here, but it </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     can never forget what they did here. It is for us </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     the living, rather, to be dedicated here to the </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     unfinished work which they who fought here have </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     thus far so nobly advanced. </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> 

 </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">

It is rather for us to be here dedicated to the </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     great task remaining before us—that from </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     these honored dead we take increased devotion to </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     that cause for which they gave the last full </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     measure of devotion—that we here highly </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     resolve that these dead shall not have died in </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     vain—that this nation, under God, shall </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     have a new birth of freedom and that government </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     of the people, by the people, for the people, </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">     shall not perish from the earth. </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> 

 </span> <br/> </div>

 <br/> </pre>
<p>Next, to help us see what&#8217;s happening during the effect, we&#8217;ll give the third paragraph a 1-pixel border and the fourth paragraph a gray background. We&#8217;ll also hide the fourth paragraph when the DOM is ready: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(2)').css('border', '1px solid #333'); <br/>  $('p:eq(3)').css('backgroundColor', '#ccc').hide(); <br/>}); <br/> </pre>
<p>Finally, we&#8217;ll add the <i>.click() </i> method to the third paragraph so that when it is clicked, the third paragraph will slide up (and out of view), while the fourth paragraph slides down (and into view): </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>$('p:eq(2)') <br/>    .css('border', '1px solid #333') <br/> <span style="font-weight: bold;">.click(function() { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      $(this).slideUp('slow') </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        .next().slideDown('slow'); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    }); </span> <br/>  $('p:eq(3)').css('backgroundColor', '#ccc').hide(); <br/>}); <br/> </pre>
<p>A screenshot of these two effects in mid-slide confirms that they do, indeed, occur virtually simultaneously: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image09.png"/> </p>
<p>The third paragraph, which started visible, is halfway through sliding up at the same time as the fourth paragraph, which started hidden, is halfway through sliding down. </p>
<h2>Callbacks </h2>
<p>In order to allow queuing effects on different elements, jQuery provides a <b>callback function </b> for each effect method. As we have seen with event handlers and with the <i>.queue() </i> method, callbacks are simply functions passed as method arguments. In the case of effects, they appear as the last argument of the method. </p>
<p>If we use a callback to queue the two slide effects, we can have the fourth paragraph slide down before the third paragraph slides up. Let&#8217;s first look at how to set up the <i>.slideDown() </i> method with the callback: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(2)') <br/>    .css('border', '1px solid #333') <br/>    .click(function() { <br/> <span style="font-weight: bold;">$(this).next().slideDown('slow',function() { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        // code here executes after 3rd paragraph's </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        // slide down has ended </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    }); </span> <br/>  }); <br/>  $('p:eq(3)').css('backgroundColor', '#ccc').hide(); <br/> }); <br/> </pre>
<p>We do need to be careful here, however, about what is actually going to slide up. The context has changed for <i>$(this) </i> because the callback is inside the <i>.slideDown() </i> method. Here, <i>$(this) </i> is no longer the third paragraph, as it was at the point of the <i>.click() </i> method; rather, since the <i>.slideDown() </i> method is attached to <i>$(this).next() </i>, everything within that method now sees <i>$(this) </i> as the next sibling, or the fourth paragraph. Therefore, if we put <i>$(this).slideUp(&#8217;slow&#8217;) </i> inside the callback, we would end up hiding the same paragraph that we had just made visible. </p>
<p>A simple way to keep the reference of <i>$(this) </i> stable is to store it in a variable right away within the <i>.click() </i> method, like <i>var $thirdPara = $(this) </i>. </p>
<p>Now <i>$thirdPara </i> will refer to the third paragraph, both outside and inside the callback. Here is what the code looks like using our new variable: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/> <span style="font-weight: bold;">var $thirdPara = $('p:eq(2)'); </span> <br/>  $thirdPara <br/>    .css('border', '1px solid #333') <br/> <span style="font-weight: bold;">.click(function() { </span> <br/>      $(this).next().slideDown('slow',function() { <br/> <span style="font-weight: bold;">$thirdPara.slideUp('slow'); </span> <br/>      }); <br/>    }); <br/>  $('p:eq(3)').css('backgroundColor', '#ccc').hide(); <br/>}); <br/> </pre>
<p>Using <i>$thirdPara </i> inside the <i>.slideDown() </i> callback relies on the properties of <b>closures </b>. </p>
<p>This time a snapshot halfway through the effects will reveal that both the third and the fourth paragraphs are visible; the fourth has finished sliding down and the third is about to begin sliding up: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image10.png"/> </p>
<p>Now that we&#8217;ve discussed callbacks, we can return to the code from earlier in this article in which we queued a background-color change near the end of a series of effects. Instead of using the <i>.queue() </i> method, as we did earlier, we can simply use a callback function: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('div.label').click(function() { <br/>    var paraWidth = $('div.speech p').outerWidth(); <br/>    var $switcher = $(this).parent(); <br/>    var switcherWidth = $switcher.outerWidth(); <br/>    $switcher <br/>      .fadeTo('slow',0.5) <br/>      .animate({ <br/>        'left': paraWidth - switcherWidth <br/>      }, 'slow') <br/>      .fadeTo('slow',1.0) <br/> <span style="font-weight: bold;">.slideUp('slow', function() { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">        $switcher </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">          .css('backgroundColor', '#f00'); </span> <br/>      }) <br/>      .slideDown('slow'); <br/>  }); <br/>}); <br/> </pre>
<p>Here again, the background color of changes to red after it slides up, and before it slides back down. </p>
<h2>In a nutshell </h2>
<p>With all the variations to consider when applying effects, it can become difficult to remember whether the effects will occur simultaneously or sequentially. A brief outline might help: </p>
<ol>
<li>Effects on a single set of elements are: <br/>&#8211; <i>simultaneous </i> when applied as multiple properties in a single <i>.animate( </i>) method. <br/> &#8212; <i>queued </i> when applied in a chain of methods, unless the queue option is set to <i>false </i>. </li>
<li>Effects on multiple sets of elements are: <br/> &#8212; <i>simultaneous </i> by default <br/> &#8212; <i>queued </i> when applied within the callback of another effect or within the callback of the <i>.queue() </i> method </li>
</ol>
<h1>Summary </h1>
<p>By using effect methods that we have explored in this article, we should now be able to incrementally increase and decrease text size by using either the <i>.css( </i>) or the <i>.animate() </i> method. We should also be able to apply various effects to gradually hide and show page elements in different ways and also to animate elements, simultaneously or sequentially, in a number of ways. </p>
<p> <br/> <br/>  <!-- end part 3 --><br />
<hr size="1" noshade="noshade" color="#ff9933"/> <br/>
<div class="header">Learning jQuery 1.3 </div>
<div style="line-height: 0.4em;">&#160; </div>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="99" valign="top"> <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank"> <img height="123" width="99" border="0" src="http://images.packtpub.com/images/100x123/1847196705.png" alt="Learning jQuery 1.3" class="left" title="Learning jQuery 1.3"/> </a> </td>
<td valign="top">
<ul>
<li>Better Interaction Design and Web Development with Simple JavaScript Techniques </li>
<li>An introduction to jQuery that requires minimal programming experience </li>
<li>Detailed solutions to specific client-side problems </li>
<li>For web designers to create interactive elements for their designs </li>
<li>For developers to create the best user interface for their web applications </li>
<li>Packed with great examples, code, and clear explanations </li>
<li>Revised and updated version of the first book to help you learn jQuery </li>
</ul>
<p>&#160;&#160; <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank">http://www.packtpub.com/learning-jquery-1.3/book </a> </td>
</tr>
</tbody>
</table>
<p> <br/><br />
<hr size="1" noshade="noshade" color="#ff9933"/> <br/> <br/><br />
<h3 class='related_post_title'>If you like this, You&#8217;ll love These.</h3>
<ul class='related_post'>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-11-19/' title='#WDNDL For 11/20/2009 – More JavaScript and CSS! Tech News &#8211; PLUS Inspiration!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 11/20/2009 – More JavaScript and CSS! Tech News &#8211; PLUS Inspiration!' title='#WDNDL For 11/20/2009 – More JavaScript and CSS! Tech News &#8211; PLUS Inspiration!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-11-19/' title='#WDNDL For 11/20/2009 – More JavaScript and CSS! Tech News &#8211; PLUS Inspiration!'>#WDNDL For 11/20/2009 – More JavaScript and CSS! Tech News &#8211; PLUS Inspiration!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-10-22/' title='#WDNDL For 10/23/2009 – Halloween Edition! Plus Photoshop &amp; jQuery Tips!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 10/23/2009 – Halloween Edition! Plus Photoshop &amp; jQuery Tips!' title='#WDNDL For 10/23/2009 – Halloween Edition! Plus Photoshop &amp; jQuery Tips!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-10-22/' title='#WDNDL For 10/23/2009 – Halloween Edition! Plus Photoshop &amp; jQuery Tips!'>#WDNDL For 10/23/2009 – Halloween Edition! Plus Photoshop &#038; jQuery Tips!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-08-04/' title='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! '><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! ' title='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! ' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-08-04/' title='#WDNDL For 8/5/2009 – Design Inspiration &amp; Tips, Photoshop Freebies, &amp; jQuery Plugins! '>#WDNDL For 8/5/2009 – Design Inspiration &#038; Tips, Photoshop Freebies, &#038; jQuery Plugins! </a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-244/' title='#WDNDL For 6/17/2009 – Wordpress, jQuery &amp; Inspiration(3D, WP, etc.)!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 6/17/2009 – Wordpress, jQuery &amp; Inspiration(3D, WP, etc.)!' title='#WDNDL For 6/17/2009 – Wordpress, jQuery &amp; Inspiration(3D, WP, etc.)!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-244/' title='#WDNDL For 6/17/2009 – Wordpress, jQuery &amp; Inspiration(3D, WP, etc.)!'>#WDNDL For 6/17/2009 – Wordpress, jQuery &#038; Inspiration(3D, WP, etc.)!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-38/' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-38/' title='AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008'>AndySowards.com :: Web Development Nerdy Daily Links For 10/23/2008</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/wdndl-for-4510-%e2%80%93-freelancing-advice-inspirations-freebies-jquery-php-oop-tips/' title='#WDNDL for 4/5/10 – Freelancing Advice, Inspirations, Freebies, jQuery, PHP &amp; OOP Tips! '><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL for 4/5/10 – Freelancing Advice, Inspirations, Freebies, jQuery, PHP &amp; OOP Tips! ' title='#WDNDL for 4/5/10 – Freelancing Advice, Inspirations, Freebies, jQuery, PHP &amp; OOP Tips! ' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/wdndl-for-4510-%e2%80%93-freelancing-advice-inspirations-freebies-jquery-php-oop-tips/' title='#WDNDL for 4/5/10 – Freelancing Advice, Inspirations, Freebies, jQuery, PHP &amp; OOP Tips! '>#WDNDL for 4/5/10 – Freelancing Advice, Inspirations, Freebies, jQuery, PHP &#038; OOP Tips! </a></span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Add Flair to your Actions with jQuery &#8211; Part 2 &#8211; Javascript Tutorial</title>
		<link>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial/</link>
		<comments>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 01:03:43 +0000</pubDate>
		<dc:creator>Packt Publishing</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[popular]]></category>
		<category><![CDATA[2]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[adding]]></category>
		<category><![CDATA[flair]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[packt]]></category>
		<category><![CDATA[packtpub]]></category>
		<category><![CDATA[packtub]]></category>
		<category><![CDATA[part]]></category>
		<category><![CDATA[part2]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.andysowards.com/blog/?p=2003</guid>
		<description><![CDATA[Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed Part 1, go ahead and read that first, or just move ahead!
Basic hide and show 
The basic .hide()  and .show()  methods, without any parameters, can be thought of as smart shorthand methods for .css(&#8216;display&#8217;,&#8217;string&#8217;) , where &#8217;string&#8217; [...]<script type="text/javascript">
Vertical1238300 = false;
ShowAdHereBanner1238300 = true;
RepeatAll1238300 = false;
NoFollowAll1238300 = false;
BannerStyles1238300 = new Array(
    "a{display:block;font-size:11px;color:#888;font-family:verdana,sans-serif;margin:0 4px 10px 0;text-align:center;text-decoration:none;overflow:hidden;}",
    "img{border:0;clear:right;}",
    "a.adhere{color:#666;font-weight:bold;font-size:12px;border:1px solid #ccc;background:#e7e7e7;text-align:center;}",
    "a.adhere:hover{border:1px solid #999;background:#ddd;color:#333;}"
);
document.write(unescape("%3Cscript src='"+document.location.protocol+"//s3.buysellads.com/1238300/1238300.js?v="+Date.parse(new Date())+"' type='text/javascript'%3E%3C/script%3E"));
</script>]]></description>
			<content:encoded><![CDATA[<p>Welcome back to part 2 of Adding Flair to your Actions with jQuery! If you missed <a href="http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-1-javascript-tutorial/">Part 1</a>, go ahead and read that first, or just move ahead!</p>
<h1>Basic hide and show </h1>
<p>The basic <i>.hide() </i> and <i>.show() </i> methods, without any parameters, can be thought of as smart shorthand methods for <i>.css(&#8216;display&#8217;,&#8217;string&#8217;) </i>, where <i>&#8217;string&#8217; </i> is the appropriate display value. The effect, as might be expected, is that the matched set of elements will be immediately hidden or shown, with no animation. </p>
<p>The <i>.hide() </i> method sets the <b>inline style attribute </b> of the matched set of elements to <i>display:none </i>. The smart part here is that it remembers the value of the <i>display </i> property&#8212;typically <i>block </i> or <i>inline </i>&#8212;before it was changed to <i>none </i>. Conversely, the <i>.show() </i> method restores the matched set of elements to whatever visible display property they had before <i>display:none </i> was applied. </p>
<p style="margin-left: 20px; margin-right: 20px;"> <em>For more information about the display property and how its values are visually represented in a web page, visit the Mozilla Developer Center at https://developer.mozilla.org/en/CSS/display/ and view examples at <a href="https://developer.mozilla.org/samples/cssref/display.html" target="_blank">https://developer.mozilla.org/samples/cssref/display.html</a>. </em> </p>
<p>This feature of <i>.show() </i> and <i>.hide() </i> is especially helpful when hiding elements whose default display property is overridden in a stylesheet. For example, the   element has the property <i>display:block </i> by default, but we might want to change it to <i>display:inline </i> for a horizontal menu. Fortunately, using the <i>.show() </i> method on a hidden element such as one of these   tags would not merely reset it to its default <i>display:block </i>, because that would put the   on its own line. Instead, the element is restored to its previous <i>display:inline </i> state, thus preserving the horizontal design. </p>
<p>A quick demonstration of these two methods can be set up by adding a second paragraph and a &#8220;read more&#8221; link after the first paragraph in the example HTML: </p>
<p> <br/>
<pre style="margin-left: 20px;">
<div id="switcher">
<div class="label">Text Size </div>

 <br/> <button id="switcher-default">Default </button> <br/> <button id="switcher-large">Bigger </button> <br/> <button id="switcher-small">Smaller </button> <br/> <br/> </div>
</pre>
<div class="speech"> <br/>
<p>Fourscore and seven years ago our fathers brought forth <br/>     on this continent a new nation, conceived in liberty, <br/>     and dedicated to the proposition that all men are created <br/>     equal. </p>
<p> <br/> </div>
<p> <br/>
<div class="speech"> <br/>
<p>Fourscore and seven years ago our fathers brought forth <br/>     on this continent a new nation, conceived in liberty, <br/>     and dedicated to the proposition that all men are <br/>     created equal. <br/> </p>
<p> <br/>
<p>Now we are engaged in a great civil war, testing whether <br/>     that nation, or any nation so conceived and so dedicated, <br/>     can long endure. We are met on a great battlefield of <br/>     that war. We have come to dedicate a portion of that <br/>     field as a final resting-place for those who here gave <br/>     their lives that the nation might live. It is altogether <br/>     fitting and proper that we should do this. But, in a <br/>     larger sense, we cannot dedicate, we cannot consecrate, <br/>     we cannot hallow, this ground. <br/> </p>
<p> <br/> <span style="font-weight: bold;"> <a href="#" class="more">read more </a> </span> <br/> </div>
<p> <br/>
<p>When the DOM is ready, the second paragraph is hidden: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(1)').hide(); <br/>}); <br/> </pre>
<p>And the speech looks like the following screenshot: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image03.png"/> </p>
<p>Then, when the user clicks on <b>read more </b> at the end of the first paragraph, that link is hidden and the second paragraph is shown: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(1)').hide(); <br/> <span style="font-weight: bold;">$('a.more').click(function() { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    $('p:eq(1)').show(); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    $(this).hide(); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    return false; </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">  }); </span> <br/>}); <br/> </pre>
<p>Note the use of return false to keep the link from activating its default action. Now the speech looks like this: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image04.png"/> </p>
<p>The <i>.hide() </i> and <i>.show() </i> methods are quick and useful, but they aren&#8217;t very flashy. To add some flair, we can give them a speed. </p>
<h2>Effects and speed </h2>
<p>When we include a <b>speed </b> (or, more precisely, a <b>duration </b>) with <i>.show() </i> or <i>.hide() </i>, it becomes animated&#8212;occurring over a specified period of time. The <i>.hide(&#8217;speed&#8217;) </i> method, for example, decreases an element&#8217;s height, width, and opacity simultaneously until all three reach zero, at which point the CSS rule <i>display:none </i> is applied. The <i>.show(&#8217;speed&#8217;) </i> method will increase the element&#8217;s height from top to bottom, width from left to right, and opacity from 0 to 1 until its contents are completely visible. </p>
<h2>Speeding in </h2>
<p>With any jQuery effect, we can use one of three preset speeds: <i>&#8217;slow&#8217; </i>, <i>&#8216;normal&#8217; </i>, and <i>&#8216;fast&#8217; </i>. Using <i>.show(&#8217;slow&#8217;) </i> makes the show effect complete in .6 seconds, <i>.show(&#8216;normal&#8217;) </i> in .4 seconds, and <i>.show(&#8216;fast&#8217;) </i> in .2 seconds. For even greater precision we can specify a number of milliseconds, for example <i>.show(850) </i>. Unlike the speed names, the numbers are not wrapped in quotation marks. </p>
<p>Let&#8217;s include a speed in our example when showing the second paragraph of Lincoln&#8217;s <b>Gettysburg Address </b>: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(1)').hide(); <br/>  $('a.more').click(function() { <br/> <span style="font-weight: bold;">$('p:eq(1)').show('slow'); </span> <br/>    $(this).hide(); <br/>    return false; <br/>  }); <br/>}); <br/> </pre>
<p>When we capture the paragraph&#8217;s appearance at roughly halfway through the effect, we see something like the following: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image05.png"/> </p>
<h2>Fading in and fading out </h2>
<p>While the animated <i>.show() </i> and <i>.hide() </i> methods are certainly flashy, they may at times be too much of a good thing. Fortunately, jQuery offers a couple other pre-built animations for a more subtle effect. For example, to have the whole paragraph appear just by gradually increasing the opacity, we can use <i>.fadeIn(&#8217;slow&#8217;) </i> instead: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(1)').hide(); <br/>  $('a.more').click(function() { <br/> <span style="font-weight: bold;">$('p:eq(1)').fadeIn('slow'); </span> <br/>    $(this).hide(); <br/>    return false; <br/>  }); <br/>}); <br/> </pre>
<p>This time when we capture the paragraph&#8217;s appearance halfway, it&#8217;s seen as: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image06.png"/> </p>
<p>The difference here is that the <i>.fadeIn() </i> effect starts by setting the dimensions of the paragraph so that the contents can simply fade into it. To gradually decrease the opacity we can use <i>.fadeOut() </i>. </p>
<hr size="1" noshade="noshade" color="#ff9933"/> <br/>
<div class="header">Learning jQuery 1.3 </div>
<div style="line-height: 0.4em;">&#160; </div>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="99" valign="top"> <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank"> <img height="123" width="99" border="0" src="http://images.packtpub.com/images/100x123/1847196705.png" alt="Learning jQuery 1.3" class="left" title="Learning jQuery 1.3"/> </a> </td>
<td valign="top">
<ul>
<li>Better Interaction Design and Web Development with Simple JavaScript Techniques </li>
<li>An introduction to jQuery that requires minimal programming experience </li>
<li>Detailed solutions to specific client-side problems </li>
<li>For web designers to create interactive elements for their designs </li>
<li>For developers to create the best user interface for their web applications </li>
<li>Packed with great examples, code, and clear explanations </li>
<li>Revised and updated version of the first book to help you learn jQuery </li>
</ul>
<p>&#160;&#160; <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank">http://www.packtpub.com/learning-jquery-1.3/book </a> </td>
</tr>
</tbody>
</table>
<p> <br/><br />
<hr size="1" noshade="noshade" color="#ff9933"/> <br/> <br/><br />
<h1>Compound effects </h1>
<p>Sometimes we have a need to toggle the visibility of elements, rather than displaying them once as we did in the previous example. Toggling can be achieved by first checking the visibility of the matched elements and then attaching the appropriate method. Using the fade effects again, we can modify the example script to look like this: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  var $firstPara = $('p:eq(1)'); <br/>  $firstPara.hide(); <br/>  $('a.more').click(function() { <br/>    if ($firstPara.is(':hidden')) { <br/>      $firstPara.fadeIn('slow'); <br/>      $(this).text('read less'); <br/>    } else { <br/>      $firstPara.fadeOut('slow'); <br/>      $(this).text('read more'); <br/>    } <br/>    return false; <br/>  }); <br/>}); <br/> </pre>
<p>As we did earlier in the article, we&#8217;re <b>caching </b> our selector here to avoid repeated DOM traversal. Notice, too, that we&#8217;re no longer hiding the clicked link; instead, we&#8217;re changing the its text. </p>
<p>Using an <i>if else </i> statement is a perfectly reasonable way to toggle elements&#8217; visibility. But with jQuery&#8217;s <b>compound effects </b> we can leave the conditionals out of it (although, in this example, we still need one for the link text). jQuery provides a <i>.toggle() </i>method, which acts like <i>.show() </i> and <i>.hide() </i>, and like them, can be used with a speed argument or without. The other compound method is <i>.slideToggle() </i>, which shows or hides elements by gradually increasing or decreasing their height. Here is what the script looks like when we use the <i>.slideToggle() </i> method: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>var $firstPara = $('p:eq(1)'); <br/>  $firstPara.hide(); <br/>  $('a.more').click(function() { <br/> <span style="font-weight: bold;">$firstPara.slideToggle('slow'); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    var $link = $(this); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    if ( $link.text() == "read more" ) { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      $link.text('read less'); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    } else { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      $link.text('read more'); </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    } </span> <br/>    return false; <br/>  }); <br/>}); <br/> </pre>
<p>This time <i>$(this) </i> would have been repeated, so we&#8217;re storing it in the $link variable for performance and readability. Also, the conditional statement checks for the text of the link rather than the visibility of the second paragraph, since we&#8217;re only using it to change the text. </p>
<h1>Creating custom animations </h1>
<p>In addition to the pre-built effect methods, jQuery provides a powerful <i>.animate() </i> method that allows us to create our own <b>custom animations </b> with fine-grained control. The <i>.animate() </i> method comes in two forms. The first takes up to four arguments: </p>
<ol>
<li>A <b>map </b> of style properties and values&#8212;similar to the <i>.css() </i> map discussed earlier in this article </li>
<li>An optional <b>speed </b>&#8212;which can be one of the preset strings or a number of milliseconds </li>
<li>An optional <b>easing type </b>&#8212;an advanced option </li>
<li>An optional callback function&#8212;which will be discussed later in this article </li>
</ol>
<p>All together, the four arguments look like this: </p>
<pre style="margin-left: 20px;">.animate({property1: 'value1', property2: 'value2'}, <br/>  speed, easing, function() { <br/>    alert('The animation is finished.'); <br/>  } <br/>); <br/> </pre>
<p>The second form takes two arguments, a <strong>map </strong> of properties and a map of options. </p>
<pre style="margin-left: 20px;">.animate({properties}, {options}) <br/> </pre>
<p>In effect, the second argument wraps up the second through fourth arguments of the first form into another map, and adds two more options to the mix. When we adjust the line breaks for readability, the second form looks like this: </p>
<pre style="margin-left: 20px;">.animate({ <br/>  property1: 'value1', <br/>  property2: 'value2' <br/>}, { <br/>  duration: 'value', <br/>  easing: 'value', <br/>  complete: function() { <br/>    alert('The animation is finished.'); <br/>  }, <br/>  queue: boolean, <br/>  step: callback <br/>}); <br/> </pre>
<p>For now we&#8217;ll use the first form of the <i>.animate() </i> method, but we&#8217;ll return to the second form later in the article when we discuss queuing effects. </p>
<h2>Toggling the fade </h2>
<p>When we discussed compound effects, did you notice that not all methods have a corresponding method for toggling? That&#8217;s right: while the sliding methods include <i>.slideToggle() </i>, there is no corresponding <i>.fadeToggle() </i> to go along with <i>.fadeIn() </i> and <i>.fadeOut() </i>! The good news is that we can use the <i>.animate() </i> method to easily make our own toggling fade animation. Here we&#8217;ll replace the <i>.slideToggle() </i> line of the previous example with our custom animation: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(1)').hide(); <br/>  $('a.more').click(function() { <br/> <span style="font-weight: bold;"> $('p:eq(1)').animate({opacity: 'toggle'}, 'slow'); </span> <br/>    var $link = $(this); <br/>    if ( $link.text() == "read more" ) { <br/>      $link.text('read less'); <br/>    } else { <br/>      $link.text('read more'); <br/>    } <br/>    return false; <br/>  }); <br/>}); <br/> </pre>
<p>As the example illustrates, the <i>.animate() </i> method provides convenient shorthand values for CSS properties &#8212; <i>&#8217;show&#8217; </i>, <i>&#8216;hide&#8217; </i>, and <i>&#8216;toggle&#8217; </i> &#8212; to ease the way when the shorthand <em>methods </em> aren&#8217;t quite right for the particular task. </p>
<h2>Animating multiple properties </h2>
<p>With the <i>.animate() </i> method, we can modify any combination of properties simultaneously. For example, to create a simultaneous sliding and fading effect when toggling the second paragraph, we simply add the <i>height </i> property-value pair to <i>.animate() </i>&#8217;s properties map: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('p:eq(1)').hide(); <br/>  $('a.more').click(function() { <br/> <span style="font-weight: bold;">$('p:eq(1)').animate({ </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      opacity: 'toggle', </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">      height: 'toggle' </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    }, </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;">    'slow'); </span> <br/>    var $link = $(this); <br/>    if ( $link.text() == "read more" ) { <br/>      $link.text('read less'); <br/>    } else { <br/>      $link.text('read more'); <br/>    } <br/>    return false; <br/>  }); <br/>}); <br/> </pre>
<p>Additionally, we have not only the style properties used for the shorthand effect methods at our disposal, but also other properties such as: <i>left </i>, <i>top </i>, <i>fontSize </i>, <i>margin </i>, <i>padding </i>, and <i>borderWidth </i>. Recall the script to change the text size of the speech paragraphs. We can animate the increase or decrease in size by simply substituting the <i>.animate() </i> method for the <i>.css() </i> method: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  var $speech = $('div.speech'); <br/>  var defaultSize = $speech.css('fontSize'); <br/>  $('#switcher button').click(function() { <br/>    var num = parseFloat( $speech.css('fontSize'), 10 ); <br/>    switch (this.id) { <br/>      case 'switcher-large': <br/>        num *= 1.4; <br/>        break; <br/>      case 'switcher-small': <br/>        num /= 1.4; <br/>        break; <br/>      default: <br/>        num = parseFloat(defaultSize, 10); <br/>    } <br/>    $speech.animate({fontSize: num + 'px'}, <br/>                                    'slow'); <br/>  }); <br/>}); <br/> </pre>
<p>The extra properties allow us to create much more complex effects, too. We can, for example, move an item from the left side of the page to the right while increasing its height by 20 pixels and changing its border width to 5 pixels. </p>
<p>So, let&#8217;s do that with the   box. Here is what it looks like before we animate it: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image07.png"/> </p>
<p>With a flexible-width layout, we need to compute the distance that the box needs to travel before it lines up at the right side of the page. Assuming that the paragraph&#8217;s width is 100%, we can subtract the <b>Text Size </b> box&#8217;s width from the paragraph&#8217;s width. While jQuery&#8217;s <i>.width() </i> method would usually come in handy for such calculations, it doesn&#8217;t factor in the width of the right and left padding or the right and left border. As of jQuery version 1.2.6, though we also have the <i>.outerWidth() </i> method at our disposal. This is what we&#8217;ll use here, to avoid having to add padding and border widths as well. For the sake of this example, we&#8217;ll trigger the animation by clicking the <b>Text Size </b> label, just above the buttons. Here is what the code should look like: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/>  $('div.label').click(function() { <br/>    var paraWidth = $('div.speech p').outerWidth(); <br/>    var $switcher = $(this).parent(); <br/>    var switcherWidth = $switcher.outerWidth(); <br/>    $switcher.animate({left: paraWidth - switcherWidth, <br/>             height: '+=20px', borderWidth: '5px'}, 'slow'); <br/>  }); <br/>}); <br/> </pre>
<p>Note that the <i>height </i> property has <i>+= </i> before the pixel value. This expression, introduced in jQuery 1.2, indicates a <b>relative value </b>. So, instead of animating the height to 20 pixels, the height is animated to 20 pixels greater than the current height. </p>
<p>Although this code successfully increases the height of the  and widens its border, at the moment the <i>left </i> position cannot be changed. We still need to enable changing its position in the CSS. </p>
<h3>Positioning with CSS </h3>
<p>When working with <i>.animate() </i>, it&#8217;s important to keep in mind the limitations that CSS imposes on the elements that we wish to change. For example, adjusting the <i>left </i> property will have no effect on the matching elements unless those elements have their CSS position set to <i>relative </i> or <i>absolute </i>. The default CSS position for all block-level elements is static, which accurately describes how those elements will remain if we try to move them without first changing their <i>position </i> value. </p>
<p style="margin-left: 20px; margin-right: 20px;"> <em>For more information on absolute and relative positioning, see Joe Gillespie&#8217;s article, Absolutely Relative at: <a href="http://www.wpdfd.com/issues/78/absolutely_relative/" target="_blank">http://www.wpdfd.com/issues/78/absolutely_relative/ </a> </em> </p>
<p>A peek at our stylesheet shows that we have now set  to be relatively positioned: </p>
<pre style="margin-left: 20px;"> #switcher { <br/>  position: relative; <br/>} <br/> </pre>
<p>With the CSS taken into account, the result of clicking on Text Size, when the animation has completed, will look like this: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image08.png"/> </p>
<hr size="1" noshade="noshade" color="#ff9933"/> <br/>
<div class="header">Learning jQuery 1.3 </div>
<div style="line-height: 0.4em;">&#160; </div>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="99" valign="top"> <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank"> <img height="123" width="99" border="0" src="http://images.packtpub.com/images/100x123/1847196705.png" alt="Learning jQuery 1.3" class="left" title="Learning jQuery 1.3"/> </a> </td>
<td valign="top">
<ul>
<li>Better Interaction Design and Web Development with Simple JavaScript Techniques </li>
<li>An introduction to jQuery that requires minimal programming experience </li>
<li>Detailed solutions to specific client-side problems </li>
<li>For web designers to create interactive elements for their designs </li>
<li>For developers to create the best user interface for their web applications </li>
<li>Packed with great examples, code, and clear explanations </li>
<li>Revised and updated version of the first book to help you learn jQuery </li>
</ul>
<p>&#160;&#160; <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank">http://www.packtpub.com/learning-jquery-1.3/book </a> </td>
</tr>
</tbody>
</table>
<p> <br/><br />
<hr size="1" noshade="noshade" color="#ff9933"/> <br/> <br/>   <!-- END PART 2 --></p>
<p>If you feel confident in what you just read, then go ahead and move on to <a href="http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/">Part 3</a>! Of course, it always helps to read things a second or third time first, just to let it sink in. Good luck! I hope you are learning some cool tricks!<br />
<h3 class='related_post_title'>If you like this, You&#8217;ll love These.</h3>
<ul class='related_post'>
<li><a href='http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/10/packt-jquery-part3-thumb.jpg' alt='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial' /> </a><span><a href='http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial'>How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/wdndl-for-2262010-%e2%80%93-inspiration-user-interface-logos-cssjqueryajax-tutorials-geek-news/' title='#WDNDL For 2/26/2010 – Inspiration, User Interface, Logos, CSS/jQuery/AJAX Tutorials &amp; Geek News!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/26/2010 – Inspiration, User Interface, Logos, CSS/jQuery/AJAX Tutorials &amp; Geek News!' title='#WDNDL For 2/26/2010 – Inspiration, User Interface, Logos, CSS/jQuery/AJAX Tutorials &amp; Geek News!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/wdndl-for-2262010-%e2%80%93-inspiration-user-interface-logos-cssjqueryajax-tutorials-geek-news/' title='#WDNDL For 2/26/2010 – Inspiration, User Interface, Logos, CSS/jQuery/AJAX Tutorials &amp; Geek News!'>#WDNDL For 2/26/2010 – Inspiration, User Interface, Logos, CSS/jQuery/AJAX Tutorials &#038; Geek News!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/wdndl-for-2222010-%e2%80%93-freelancer-blogging-tips-jquery-stuff-plus-wp-theme-template-freebies/' title='#WDNDL For 2/22/2010 – Freelancer &amp; Blogging Tips, jQuery Stuff, Plus WP Theme &amp; Template Freebies!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/22/2010 – Freelancer &amp; Blogging Tips, jQuery Stuff, Plus WP Theme &amp; Template Freebies!' title='#WDNDL For 2/22/2010 – Freelancer &amp; Blogging Tips, jQuery Stuff, Plus WP Theme &amp; Template Freebies!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/wdndl-for-2222010-%e2%80%93-freelancer-blogging-tips-jquery-stuff-plus-wp-theme-template-freebies/' title='#WDNDL For 2/22/2010 – Freelancer &amp; Blogging Tips, jQuery Stuff, Plus WP Theme &amp; Template Freebies!'>#WDNDL For 2/22/2010 – Freelancer &#038; Blogging Tips, jQuery Stuff, Plus WP Theme &#038; Template Freebies!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-02/' title='#WDNDL For 2/3/2010 – JS, CSS3, &amp; Photoshop Tutorials, Inspirations, &amp; Tech News!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/3/2010 – JS, CSS3, &amp; Photoshop Tutorials, Inspirations, &amp; Tech News!' title='#WDNDL For 2/3/2010 – JS, CSS3, &amp; Photoshop Tutorials, Inspirations, &amp; Tech News!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-02/' title='#WDNDL For 2/3/2010 – JS, CSS3, &amp; Photoshop Tutorials, Inspirations, &amp; Tech News!'>#WDNDL For 2/3/2010 – JS, CSS3, &#038; Photoshop Tutorials, Inspirations, &#038; Tech News!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-01/' title='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!' title='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-01/' title='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!'>#WDNDL For 2/2/2010 – Apple News, Photoshop &#038; Code Tuts, Freebies &#038; Inspirations!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-25/' title='#WDNDL For 1/26/2010 – JS, Wordpress, &amp; jQuery Resources! With Inspirations!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 1/26/2010 – JS, Wordpress, &amp; jQuery Resources! With Inspirations!' title='#WDNDL For 1/26/2010 – JS, Wordpress, &amp; jQuery Resources! With Inspirations!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-25/' title='#WDNDL For 1/26/2010 – JS, Wordpress, &amp; jQuery Resources! With Inspirations!'>#WDNDL For 1/26/2010 – JS, Wordpress, &#038; jQuery Resources! With Inspirations!</a></span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Add Flair to your Actions with jQuery &#8211; Part 1 &#8211; Javascript Tutorial</title>
		<link>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-1-javascript-tutorial/</link>
		<comments>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-1-javascript-tutorial/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 08:48:54 +0000</pubDate>
		<dc:creator>Packt Publishing</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[popular]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[packt]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[publishing]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.andysowards.com/blog/?p=1942</guid>
		<description><![CDATA[How to Add Flair to your Actions with jQuery 
If actions speak louder than words, then in the JavaScript world, effects make actions speak louder still. With jQuery, we can easily add impact to our actions through a set of simple visual effects, and even craft our own, more sophisticated animations . 
jQuery effects certainly [...]<script type="text/javascript">
Vertical1238300 = false;
ShowAdHereBanner1238300 = true;
RepeatAll1238300 = false;
NoFollowAll1238300 = false;
BannerStyles1238300 = new Array(
    "a{display:block;font-size:11px;color:#888;font-family:verdana,sans-serif;margin:0 4px 10px 0;text-align:center;text-decoration:none;overflow:hidden;}",
    "img{border:0;clear:right;}",
    "a.adhere{color:#666;font-weight:bold;font-size:12px;border:1px solid #ccc;background:#e7e7e7;text-align:center;}",
    "a.adhere:hover{border:1px solid #999;background:#ddd;color:#333;}"
);
document.write(unescape("%3Cscript src='"+document.location.protocol+"//s3.buysellads.com/1238300/1238300.js?v="+Date.parse(new Date())+"' type='text/javascript'%3E%3C/script%3E"));
</script>]]></description>
			<content:encoded><![CDATA[<h2>How to Add Flair to your Actions with jQuery </h2>
<blockquote><p>If actions speak louder than words, then in the JavaScript world, effects make actions speak louder still. With jQuery, we can easily add impact to our actions through a set of simple visual effects, and even craft our own, more sophisticated <b>animations </b>. </p>
<p>jQuery effects certainly add flair, as is evident when we see elements gradually slide into view instead of appearing all at once. However, they can also provide important usability enhancements that help orient the user when there is some change on a page (especially common in AJAX applications). In this article by <b>Jonathan Chaffer </b> and <b>Karl Swedberg </b>, we will explore a number of these effects and combine them in interesting ways. </p>
</blockquote>
<h1>Inline CSS modification </h1>
<p>Before we jump into the nifty jQuery effects, a quick look at CSS is in order. One way of modifying a document&#8217;s appearance is by defining styles for classes in a separate stylesheet and then adding or removing those classes with jQuery. Typically, this is the preferred process for injecting CSS into HTML because it respects the stylesheet&#8217;s role in dealing with the presentation of a page. However, there may be times when we need to apply styles that haven&#8217;t been, or can&#8217;t easily be, defined in a stylesheet. Fortunately, jQuery offers the <i>.css() </i> method for such occasions. </p>
<p>This method acts as both a <b>getter </b> and a <b>setter </b>. To get the value of a style property, we simply pass the name of the property as a string, like <i>.css(&#8216;backgroundColor&#8217;) </i>. Multi-word properties can be interpreted by jQuery when hyphenated, as they are in CSS notation ( <i>background-color </i>), or camel-cased, as they are in DOM notation ( <i>backgroundColor </i>). For setting style properties, the <i>.css() </i> method comes in two flavors&#8212;one that takes a single style property and its value and one that takes a <b>map </b> of property-value pairs: </p>
<pre style="margin-left: 20px;">.css('property','value') <br/>.css({property1: 'value1', 'property-2': 'value2'}) <br/> </pre>
<p>Experienced JavaScript developers will recognize these jQuery maps as JavaScript <b>object literals </b>. </p>
<p style="margin-left: 20px; margin-right: 20px;"> <em>Numeric values do not take quotation marks while string values do. However, when using the map notation, quotation marks are not required for property names if they are written in camel-cased DOM notation. </em> </p>
<p>We use the <i>.css() </i> method the same way as using <i>.addClass() </i> &#8212;by <b>chaining </b> it to a selector and <b>binding </b> it to an event. To demonstrate this, we&#8217;ll use the style switcher example. </p>
<pre style="margin-left: 20px;">
<div id="switcher"> <br/>
<div class="label">Text Size </div>

 <br/> <button id="switcher-default">Default </button> <br/> <button id="switcher-large">Bigger </button> <br/> <button id="switcher-small">Smaller </button> <br/> </div>

 <br/>
<div class="speech"> <br/>

Fourscore and seven years ago our fathers brought forth <br/> on this continent a new nation, conceived in liberty, <br/> and dedicated to the proposition that all men are created <br/> equal. 

 <br/> </div>

 <br/> </pre>
<p>By linking to a stylesheet with a few basic style rules, the page can initially look like the following screenshot: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image01.png"/> </p>
<p>In this version of the style switcher, we&#8217;re using <button id="switcher-large">- </button> elements. Clicking on the <b>Bigger </b> and <b>Smaller </b> buttons will increase or decrease the text size of , while clicking on the <b>Default </b> button will reset to its original text size. </p>
<p>If all we wanted were to change the font size a single time to a predetermined value, we could still use the <i>.addClass() </i> method. But let&#8217;s suppose that now we want the text to continue increasing or decreasing incrementally each time the respective button is clicked. Although it might be possible to define a separate class for each click and iterate through them, a more straightforward approach would be to compute the new text size each time by getting the current size and increasing it by a set factor (for example, 40%). </p>
<p>Our code will start with the <i>$(document).ready() </i> and <i>$(&#8216;#switcher-large&#8217;) </i>.click() event handlers: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/> $('#switcher-large').click(function() { <br/> }); <br/>}); <br/> </pre>
<p>Next, the font size can be easily discovered by using the <i>.css() </i> method: <i>$(&#8216;div.speech&#8217;).css(&#8216;fontSize&#8217;). </i> However, because the returned value will include a trailing &#8216; <i>px </i>&#8216;, we&#8217;ll need to strip that part in order to perform calculations with the value. Also, when we plan to use a jQuery object more than once, it&#8217;s generally a good idea to <b>cache </b> the selector by storing the resulting jQuery object in a variable as well. </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/> <span style="font-weight: bold;">var $speech = $('div.speech'); </span> <br/> $('#switcher-large').click(function() { <br/> <span style="font-weight: bold;">var num = parseFloat($speech.css('fontSize'), 10); </span> <br/> }); <br/>}); <br/> </pre>
<p>The first line inside <i>$(document).ready() </i> now stores a variable for itself. Notice the use of a <i>$ </i> in the variable name, <i>$speech </i>. Since <i>$ </i> is a legal character in JavaScript variables, we can use it as a reminder that the variable is storing a jQuery object. </p>
<p>Inside the <i>.click() </i> handler, we use <i>parseFloat() </i> to get the font size property&#8217;s number only. The <i>parseFloat() </i> function looks at a string from left to right until it encounters a non-numeric character. The string of digits is converted into a floating-point (decimal) number. For example, it would convert the string &#8216; <i>12&#8242; </i> to the number 12. In addition, it strips non-numeric trailing characters from the string, so <i>&#8216;12px&#8217; </i> becomes 12 as well. If the string begins with a non-numeric character, <i>parseFloat() </i> returns NaN, which stands for <b>Not a Number </b>. The second argument for <i>parseFloat() </i> allows us to ensure that the number is interpreted as base-10 instead of octal or some other representation. </p>
<p>All that&#8217;s left to do, if we are increasing by 40%, is to multiply <i>num </i> by 1.4 and then set the font size by concatenating <i>num </i> and <i>&#8216;px&#8217; </i>: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/> var $speech = $('div.speech'); <br/> $('#switcher-large').click(function() { <br/> var num = parseFloat($speech.css('fontSize'), 10 ); <br/> <span style="font-weight: bold;">num *= 1.4; </span> <br/> <span style="font-weight: bold;">$speech.css('fontSize', num + 'px'); </span> <br/> }); <br/>}); <br/> </pre>
<p style="margin-left: 20px; margin-right: 20px;"> <em>The equation num *= 1.4 is shorthand for num = num * 1.4. We can use the same type of shorthand for the other basic mathematical operations, as well: addition, num += 1.4; subtraction, num -= 1.4; division, num /= 1.4; and modulus (division remainder), num %= 1.4. </em> </p>
<p>Now when a user clicks on the <b>Bigger </b> button, the text becomes larger. Another click, and the text becomes larger still, as shown in the following screenshot: </p>
<p style="text-align: center;"> <img src="http://www.packtpub.com/files/images/jquery_article1_image02.png"/> </p>
<p>To get the <b>Smaller </b> button to decrease the font size, we will divide rather than multiply <i>&#8212;num /= 1.4. </i> Better still, we&#8217;ll combine the two into a single <i>.click() </i> handler on all <button id="switcher-large">- </button> elements within . Then, after finding the numeric value, we can either multiply or divide depending on the ID of the button that was clicked. Here is what that code looks like now: </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/> var $speech = $('div.speech'); <br/> <span style="font-weight: bold;">$('#switcher button').click(function() { </span> <br/> var num = parseFloat( $speech.css('fontSize'), 10 ); <br/> <span style="font-weight: bold;">if (this.id == 'switcher-large') { </span> <br/> <span style="font-weight: bold;">num *= 1.4; </span> <br/> <span style="font-weight: bold;">} else if (this.id == 'switcher-small') { </span> <br/> <span style="font-weight: bold;">num /= 1.4; </span> <br/> } <br/> $speech.css('fontSize', num + 'px); <br/> }); <br/>}); <br/> </pre>
<p>We can access the <i>id </i> property of the DOM element referred to by <i>this </i>, which appears here inside the <i>if </i> and <i>else if </i> statements. Here, it is more efficient to use <i>this </i> than to create a jQuery object just to test the value of a property. </p>
<p>It&#8217;s also nice to have a way to return the font size to its initial value. To allow the user to do so, we can simply store the font size in a variable immediately when the DOM is ready. We can then use this value whenever the <b>Default </b> button is clicked. To handle this click, we could add another <i>else if </i> statement. However, perhaps a <i>switch </i> statement would be more appropriate. </p>
<pre style="margin-left: 20px;">$(document).ready(function() { <br/> var $speech = $('div.speech'); <br/> <span style="font-weight: bold;">var defaultSize = $speech.css('fontSize'); </span> <br/> $('#switcher button').click(function() { <br/> var num = parseFloat( $speech.css('fontSize'), 10 ); <br/> <span style="font-weight: bold;">switch (this.id) { </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> case 'switcher-large': </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> num *= 1.4; </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> break; </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> case 'switcher-small': </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> num /= 1.4; </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> break; </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> default: </span> <br style="font-weight: bold;"/> <span style="font-weight: bold;"> num = parseFloat(defaultSize, 10); </span> <br/> <br/> $speech.css('fontSize', num + 'px'); <br/> }); <br/>}); <br/> </pre>
<p>Here we&#8217;re still checking the value of <i>this.id </i> and changing the font size based on it, but if its value is neither <i>&#8217;switcher-large&#8217; </i> nor <i>&#8217;switcher-small&#8217; </i> it will default to the initial font size. </p>
<p> <!-- END PART 1 --></p>
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="99" valign="top"> <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank"><img height="123" width="99" border="0" src="http://images.packtpub.com/images/100x123/1847196705.png" alt="Learning jQuery 1.3" class="left" title="Learning jQuery 1.3"/></a></td>
<td valign="top">
<ul>
<li>Better Interaction Design and Web Development with Simple JavaScript Techniques</li>
<li>An introduction to jQuery that requires minimal programming experience</li>
<li>Detailed solutions to specific client-side problems</li>
<li>For web designers to create interactive elements for their designs</li>
<li>For developers to create the best user interface for their web applications</li>
<li>Packed with great examples, code, and clear explanations</li>
<li>Revised and updated version of the first book to help you learn jQuery</li>
</ul>
<p>  <a href="http://www.packtpub.com/learning-jquery-1.3/book/mid/240609o1fp7w" target="_blank">http://www.packtpub.com/learning-jquery-1.3/book</a> </td>
</tr>
</tbody>
</table>
<p>Hope you learned something in Part I, Now move on to <a href="http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-2-javascript-tutorial/">Part II of the Adding Flare to your Actions with jQuery series</a>!<br />
<h3 class='related_post_title'>If you like this, You&#8217;ll love These.</h3>
<ul class='related_post'>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-05/' title='#WDNDL For 2/8/2010 – Navigation, UX/UI, jQuery, CSS3, AJAX, Javascript, &amp; Freelancer Tips/Tutorials!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/8/2010 – Navigation, UX/UI, jQuery, CSS3, AJAX, Javascript, &amp; Freelancer Tips/Tutorials!' title='#WDNDL For 2/8/2010 – Navigation, UX/UI, jQuery, CSS3, AJAX, Javascript, &amp; Freelancer Tips/Tutorials!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-05/' title='#WDNDL For 2/8/2010 – Navigation, UX/UI, jQuery, CSS3, AJAX, Javascript, &amp; Freelancer Tips/Tutorials!'>#WDNDL For 2/8/2010 – Navigation, UX/UI, jQuery, CSS3, AJAX, Javascript, &#038; Freelancer Tips/Tutorials!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-01/' title='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!' title='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-01/' title='#WDNDL For 2/2/2010 – Apple News, Photoshop &amp; Code Tuts, Freebies &amp; Inspirations!'>#WDNDL For 2/2/2010 – Apple News, Photoshop &#038; Code Tuts, Freebies &#038; Inspirations!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-28/' title='#WDNDL For 1/29/2010 – More iPad News, Freebies, Inspiration, &amp; Web Apps!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 1/29/2010 – More iPad News, Freebies, Inspiration, &amp; Web Apps!' title='#WDNDL For 1/29/2010 – More iPad News, Freebies, Inspiration, &amp; Web Apps!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-28/' title='#WDNDL For 1/29/2010 – More iPad News, Freebies, Inspiration, &amp; Web Apps!'>#WDNDL For 1/29/2010 – More iPad News, Freebies, Inspiration, &#038; Web Apps!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-19/' title='#WDNDL For 01/20/2010 – HUGE &#8211; Freebies(font, psd), eCommerce, Inspiration &amp; More!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 01/20/2010 – HUGE &#8211; Freebies(font, psd), eCommerce, Inspiration &amp; More!' title='#WDNDL For 01/20/2010 – HUGE &#8211; Freebies(font, psd), eCommerce, Inspiration &amp; More!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-01-19/' title='#WDNDL For 01/20/2010 – HUGE &#8211; Freebies(font, psd), eCommerce, Inspiration &amp; More!'>#WDNDL For 01/20/2010 – HUGE &#8211; Freebies(font, psd), eCommerce, Inspiration &#038; More!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-12-05/' title='#WDNDL For 12/07/2009 – Nerd Humor, Inspirations, &amp; News &#8211; Plus JS/CSS Tips!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 12/07/2009 – Nerd Humor, Inspirations, &amp; News &#8211; Plus JS/CSS Tips!' title='#WDNDL For 12/07/2009 – Nerd Humor, Inspirations, &amp; News &#8211; Plus JS/CSS Tips!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-12-05/' title='#WDNDL For 12/07/2009 – Nerd Humor, Inspirations, &amp; News &#8211; Plus JS/CSS Tips!'>#WDNDL For 12/07/2009 – Nerd Humor, Inspirations, &#038; News &#8211; Plus JS/CSS Tips!</a></span></li>
<li><a href='http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/10/packt-jquery-part3-thumb.jpg' alt='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial' /> </a><span><a href='http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-%e2%80%93-part-3-%e2%80%93-javascript-tutorial/' title='How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial'>How to Add Flair to your Actions with jQuery – Part 3 – Javascript Tutorial</a></span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andysowards.com/blog/tutorials/how-to-add-flair-to-your-actions-with-jquery-part-1-javascript-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy Javascript/AJAX Exit Pop up script &#8211; Creates div overlay on page exit</title>
		<link>http://www.andysowards.com/blog/tutorials/easy-javascriptajax-exit-pop-up-script-creates-div-overlay-on-page-exit/</link>
		<comments>http://www.andysowards.com/blog/tutorials/easy-javascriptajax-exit-pop-up-script-creates-div-overlay-on-page-exit/#comments</comments>
		<pubDate>Sun, 31 Aug 2008 23:30:21 +0000</pubDate>
		<dc:creator>Andy Sowards</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[popular]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Easy Javascript]]></category>
		<category><![CDATA[Exit Pop]]></category>
		<category><![CDATA[Exit Traffic]]></category>
		<category><![CDATA[Exit Traffic Scripts]]></category>
		<category><![CDATA[free script]]></category>
		<category><![CDATA[Javascript/AJAX]]></category>
		<category><![CDATA[On Exit Pop Up]]></category>
		<category><![CDATA[Salvage Leads]]></category>
		<category><![CDATA[Salvage Sales]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[time saver]]></category>
		<category><![CDATA[Traffic]]></category>
		<category><![CDATA[Traffic Capture]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[webmaster tools]]></category>
		<category><![CDATA[XML HTTP REQUEST]]></category>
		<category><![CDATA[xmlHttp]]></category>

		<guid isPermaLink="false">http://www.andysowards.com/blog/?p=148</guid>
		<description><![CDATA[So the other day a client requested that when a visitor leaves any page on the site, they be greeted by an alert (such as &#8216;alert(&#8216;this is your alert&#8217;);&#8217;) and if they click &#8216;Cancel&#8217; they then go to an alternate &#8216;Exit Pop&#8217; page that acts as a last ditch effort page to salvage a sale, [...]<script type="text/javascript">
Vertical1238300 = false;
ShowAdHereBanner1238300 = true;
RepeatAll1238300 = false;
NoFollowAll1238300 = false;
BannerStyles1238300 = new Array(
    "a{display:block;font-size:11px;color:#888;font-family:verdana,sans-serif;margin:0 4px 10px 0;text-align:center;text-decoration:none;overflow:hidden;}",
    "img{border:0;clear:right;}",
    "a.adhere{color:#666;font-weight:bold;font-size:12px;border:1px solid #ccc;background:#e7e7e7;text-align:center;}",
    "a.adhere:hover{border:1px solid #999;background:#ddd;color:#333;}"
);
document.write(unescape("%3Cscript src='"+document.location.protocol+"//s3.buysellads.com/1238300/1238300.js?v="+Date.parse(new Date())+"' type='text/javascript'%3E%3C/script%3E"));
</script>]]></description>
			<content:encoded><![CDATA[<p>So the other day a client requested that when a visitor leaves any page on the site, they be greeted by an alert (such as &#8216;alert(&#8216;this is your alert&#8217;);&#8217;) and if they click &#8216;Cancel&#8217; they then go to an alternate &#8216;Exit Pop&#8217; page that acts as a<em> last ditch effort</em> page to salvage a sale, or an opt-in, or dignity, what have you. Otherwise they just click &#8216;OK&#8217; and go on their merry way in the opposite direction.</p>
<blockquote><p>Usually I would just grab code from another page that I had done something similar on, but If you have ever dealt with old code before, You have either forgotten exactly what you needed to change in order for it to work &#8216;out of the box&#8217; for your new situation, so you end up wasting about 10 minutes testing to get it right.</p>
<p>So I decided to streamline the process once and for all!</p></blockquote>
<p>I had searched around the Googleverse for a similar simple script, but could not find an easy to use one that was contained in basically one easy to use file that did not include a kitchen sink of un-needed features.</p>
<p>I will try to explain my code. So here we go:<br />
<span id="more-148"></span><br />
First, the page you want the action to happen on, in our case, is index.php</p>
<p>This is the only important bit of code needed on that page.</p>
<p><code>&lt;script type="text/javascript" src="scripts/as_exitPop.js"&gt;<br />
/***********************************************<br />
|A|n|d|y|S|o|w|a|r|d|s|.|c|o|m|<br />
------------------------------------------<br />
* Easy Javascript/PHP Exit Pop up script @ AndySowards.com Developer's Blog (www.andysowards.com)<br />
* This notice MUST stay intact for legal use<br />
* Visit Andy Sowards at http://www.andysowards.com/ for full source code<br />
***********************************************/<br />
&lt;/script&gt;</code></p>
<p>This is of course the code that includes the .js file(which will do all of our work) and will of course preserve my copyright information(which is creative commons, so basically you can do whatever you want with it, but please give me credit for it.). I usually place this code before the ending tag, but I have not tried it in the head code. Give me feedback if you try it that way!</p>
<p>Next is the actual .js file, which is called &#8216;as_exitPop.js&#8217; and is located in the &#8217;scripts&#8217; folder:</p>
<p><code><br />
//EDIT HERE ONLY</code></p>
<p>var ExitPopURL = &#8216;http://www.andysowards.com/dev/exitpop/exit.php&#8217;; //This is the URL where your &#8216;exit page&#8217; is located.</p>
<p>/* NOTE: If you experience an error it is most likely due to the strict AJAX security, make sure that you are accessing the correct URL, for example, if you have http://domain.com in your browser, and http://www.domain.com in the &#8216;ExitPopURL&#8217; then there will be a conflict. they must both match. .htaccess to ensure that your visitors are visiting www. is good practice here.*/</p>
<p>var AlertBox = &#8220;*****************************************************\n\nWait! Stop! Don&#8217;t Go!\n\nBefore leaving, please leave a comment on the \n\nscript post and share your suggestions.\n\nThanks for visiting AndySowards.com.\n\nAndy \n\n*****************************************************&#8221;;</p>
<p>// This is what the alert() pop up verbage says.</p>
<p>//DO NOT EDIT BELOW This LINE (Unless of course your Savvy!) &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>There is more to this .js file of course, but for the moment, this is all you really need to worry about.</p>
<p>First you see a var called &#8216;ExitPopURL&#8217; and Hopefully is self explanitory to what it is. all you have to do is edit this in the .js file to be the url that points to your &#8216;exit pop&#8217; page. Which means this is the page that will be seen if the user chooses to give you a &#8217;second chance&#8217;.</p>
<p>Second, you see a variable called &#8216;AlertBox&#8217;, Again, hopefully self explanitory, but this is the variable that contains the text that will be seen in the javascript pop up alert box. You can change this to any verbage you would like.</p>
<p>Now for the next bit of code:</p>
<p><code><br />
window.onload = function(){<br />
// this is where we start our journey...<br />
createExitPop();<br />
}// end function onunload<br />
</code></p>
<p>What is happening here is that the createExitPop(); function(which is declared next) is running when the browser first loads the page. So basically when the page loads, your exit pop is hiding, like a tiger, waiting for someone to try and leave.</p>
<p>Next is your basic AJAX function, I will not explain this here because there are countless references and explanations of AJAX elsewhere:</p>
<p><code><br />
function ajaxGET(divId, page, effect)<br />
{<br />
var xmlHttp;<br />
try<br />
{<br />
// Firefox, Opera 8.0+, Safari<br />
xmlHttp=new XMLHttpRequest();<br />
}<br />
catch(e)<br />
{<br />
// Internet Explorer<br />
try<br />
{<br />
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");<br />
}<br />
catch(e)<br />
{<br />
try<br />
{<br />
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<br />
}<br />
catch(e)<br />
{<br />
alert("Your browser does not support AJAX!");<br />
return false;<br />
}<br />
}<br />
} </code></p>
<p>xmlHttp.onreadystatechange=function()<br />
{<br />
if(xmlHttp.readyState==4)<br />
{<br />
if(effect == &#8216;collapse&#8217;) { document.getElementById(divId).style.display=&#8217;none&#8217;; }<br />
else { document.getElementById(divId).innerHTML=xmlHttp.responseText; }<br />
}<br />
}<br />
xmlHttp.open(&#8220;GET&#8221;,page,true);<br />
xmlHttp.send(null);<br />
}</p>
<p>Next, Lets declare that createExitPop function:</p>
<p><code><br />
function createExitPop()<br />
{<br />
var theBody = document.getElementsByTagName('body')[0];<br />
var newdiv = document.createElement('div');<br />
newdiv.setAttribute('id','ExitDiv');<br />
theBody.setAttribute('id','body');<br />
newdiv.setAttribute('style', 'width: 100%; height: 100%;');</code></p>
<p>// put div on page<br />
theBody.appendChild(newdiv);</p>
<p>//add exit pop to page (contents are from your exit.php(or whatever you named it) page)<br />
document.getElementById(&#8216;ExitDiv&#8217;).value = ajaxGET(&#8216;ExitDiv&#8217;, ExitPopURL);</p>
<p>// style exit pop to resemble its own page<br />
document.getElementById(&#8216;ExitDiv&#8217;).style.display = &#8220;none&#8221;;<br />
document.getElementById(&#8216;ExitDiv&#8217;).style.top = &#8216;0px&#8217;;<br />
document.getElementById(&#8216;ExitDiv&#8217;).style.left = &#8216;0px&#8217;;<br />
document.getElementById(&#8216;ExitDiv&#8217;).style.position = &#8216;relative&#8217;;<br />
document.getElementById(&#8216;ExitDiv&#8217;).style.backgroundColor = &#8216;#FFFFFF&#8217;;</p>
<p>}// end createExitPop</p>
<p>Here we are taking the body tag, assigning it to the &#8216;theBody&#8217; variable and then setting an attribute to reference it, in this case we are setting the id of &#8216;body&#8217;.</p>
<p>Then we are creating a whole new div and calling the variable holding it &#8216;newdiv&#8217;. We then do the same thing with the new div that we did with the body tag, and add an attribute, an id of &#8216;ExitDiv&#8217;.<br />
We also set some styles for our new div, in this case we set the width and height.</p>
<p>We then append the new div child to the body of the document. Once our new &#8216;ExitDiv&#8217; is in the document, we can reference it using the DOM. We are using AJAX to inject our &#8216;exit.php&#8217; contents into the div using the variable &#8216;ExitPopURL&#8217; as the reference for content.</p>
<p>We then just add some styles and customize our ExitDiv.</p>
<p>We are almost done, Here is where the magic happens!</p>
<p><code><br />
isExit = true;</code></p>
<p>function ExitPop(isExit) {<br />
if(isExit != false) {<br />
isExit=false;<br />
isPop = true;</p>
<p>var bodyTag = document.getElementById? document.getElementsByTagName(&#8220;BODY&#8221;)[0] : document.body;</p>
<p>// add id=&#8221;body&#8221; so that it can be referenced.<br />
bodyTag.setAttribute(&#8220;id&#8221;, &#8220;body&#8221;);</p>
<p>//replace body text with exit pop<br />
bodyTag.innerHTML = document.getElementById(&#8216;ExitDiv&#8217;).innerHTML;<br />
return AlertBox;<br />
} // end if<br />
}// end function</p>
<p>window.onbeforeunload = function(){</p>
<p>// Lay down an exit pop!!<br />
return ExitPop(isExit);</p>
<p>}// end function onunload</p>
<p>Here we set the variable &#8216;isExit&#8217; to true. Why? Because if they try to do anything other than access an &#8216;isExit=false&#8217; tagged link or form, then they are trying to leave the page! And we cant have that now can we? Of course not.</p>
<p>Inside the ExitPop function its parameter is the &#8216;isExit&#8217; variable, it is evaluating whether or not it is true or false. If true, then the function replaces whatever is in the body tag with the inner HTML of the ExitDiv, which in turn makes the page have the content of your &#8216;exit.php&#8217; page. An alert then pops up asking the user what they want to do, and you select the copy that goes in there using the &#8216;AlertBox&#8217; variable.</p>
<p>Then, with the magic of the window.onbeforeunload method, before the window loads another page, it calls our ExitPop function and references the current state of the &#8216;isExit&#8217; Variable.</p>
<p>Once the alert pops up, If they choose &#8216;OK&#8217; they go. If they choose &#8216;Cancel&#8217; they stay. Simple right?</p>
<p>Lastly, the only thing left is to make your &#8216;exit.php&#8217; page look however you want! and the page will replace the with whatever is in your &#8216;exit.php&#8217; file, and of course you can name the &#8216;exit.php&#8217; file whatever you want.</p>
<p>One last note, if you have navigational links on your page that need to NOT trigger the exit pop functionality, simply add this attribute to the link &#8216;onClick=&#8221;isExit = false;&#8221;&#8216; or if you are using a form, then it would be &#8216;onSubmit=&#8221;isExit = false;&#8221;&#8216; and you should sneak past the security and on to the desired page <img src='http://www.andysowards.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Hope you enjoy this and it helps save you time or make you money, and if you run into any trouble or have suggestions on making it better, let me know and I will implement the good ones!</p>
<p>For a <a title="AndySowards.com - Easy Javascript/AJAX Exit Pop up Script Live Demo" href="http://www.andysowards.com/dev/exitpop/" target="_blank">Live Demo Example Click Here</a>.</p>
<p>To download the original source code click here <a title="AndySowards.com - Easy Javascript Exit Pop up script - Creates div overlay on page exit" href="http://www.andysowards.com/blog/wp-content/themes/andysowards/asdloads/AndySowards.com-Exit_Pop_Script.zip">Easy Javascript/AJAX Exit Pop up script</a><br />
<h3 class='related_post_title'>If you like this, You&#8217;ll love These.</h3>
<ul class='related_post'>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-3/' title='AndySowards.com :: Web Development Nerdy Daily Links'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links' title='AndySowards.com :: Web Development Nerdy Daily Links' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-3/' title='AndySowards.com :: Web Development Nerdy Daily Links'>AndySowards.com :: Web Development Nerdy Daily Links</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-09/' title='#WDNDL For 2/10/2010 – JS/AJAX Tuts, Design Inspiration &amp; Tuts, Redesigns, Math &amp; More!'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 2/10/2010 – JS/AJAX Tuts, Design Inspiration &amp; Tuts, Redesigns, Math &amp; More!' title='#WDNDL For 2/10/2010 – JS/AJAX Tuts, Design Inspiration &amp; Tuts, Redesigns, Math &amp; More!' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2010-02-09/' title='#WDNDL For 2/10/2010 – JS/AJAX Tuts, Design Inspiration &amp; Tuts, Redesigns, Math &amp; More!'>#WDNDL For 2/10/2010 – JS/AJAX Tuts, Design Inspiration &#038; Tuts, Redesigns, Math &#038; More!</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-12-15/' title='#WDNDL For 12/16/2009 – Scripts! JS/CSS Tutorials! Inspirations! &amp; How MMORPG&#8217;s Work? *Awesome*'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='#WDNDL For 12/16/2009 – Scripts! JS/CSS Tutorials! Inspirations! &amp; How MMORPG&#8217;s Work? *Awesome*' title='#WDNDL For 12/16/2009 – Scripts! JS/CSS Tutorials! Inspirations! &amp; How MMORPG&#8217;s Work? *Awesome*' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/links-for-2009-12-15/' title='#WDNDL For 12/16/2009 – Scripts! JS/CSS Tutorials! Inspirations! &amp; How MMORPG&#8217;s Work? *Awesome*'>#WDNDL For 12/16/2009 – Scripts! JS/CSS Tutorials! Inspirations! &#038; How MMORPG&#8217;s Work? *Awesome*</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-135/' title='AndySowards.com :: Web Development Nerdy Daily Links For 2/06/2009'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links For 2/06/2009' title='AndySowards.com :: Web Development Nerdy Daily Links For 2/06/2009' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-135/' title='AndySowards.com :: Web Development Nerdy Daily Links For 2/06/2009'>AndySowards.com :: Web Development Nerdy Daily Links For 2/06/2009</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-18/' title='AndySowards.com :: Web Development Nerdy Daily Links For 09/29/2008'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links For 09/29/2008' title='AndySowards.com :: Web Development Nerdy Daily Links For 09/29/2008' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-18/' title='AndySowards.com :: Web Development Nerdy Daily Links For 09/29/2008'>AndySowards.com :: Web Development Nerdy Daily Links For 09/29/2008</a></span></li>
<li><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-4/' title='AndySowards.com :: Web Development Nerdy Daily Links'><img  class='rp-thumb' src='http://www.andysowards.com/blog/wp-content/uploads/2009/07/redesign-launch-thumb.jpg' alt='AndySowards.com :: Web Development Nerdy Daily Links' title='AndySowards.com :: Web Development Nerdy Daily Links' /> </a><span><a href='http://www.andysowards.com/blog/nerdy-daily-web-development-design-links/daily-links-4/' title='AndySowards.com :: Web Development Nerdy Daily Links'>AndySowards.com :: Web Development Nerdy Daily Links</a></span></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.andysowards.com/blog/tutorials/easy-javascriptajax-exit-pop-up-script-creates-div-overlay-on-page-exit/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
	</channel>
</rss>
