<?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>tobypitman.com &#187; jQuery</title>
	<atom:link href="http://www.tobypitman.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tobypitman.com</link>
	<description>Official website of guitarist Toby Pitman</description>
	<lastBuildDate>Thu, 24 Jun 2010 14:24:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multiple collapsable panels with cookies</title>
		<link>http://www.tobypitman.com/multiple-collapsable-panels-with-cookies/</link>
		<comments>http://www.tobypitman.com/multiple-collapsable-panels-with-cookies/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 12:30:36 +0000</pubDate>
		<dc:creator>Toby</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.tobypitman.com/?p=154</guid>
		<description><![CDATA[There's literally nothing about on the internet about using the jQuery toogle() function on multiple collapsable panels and saving their states with cookies from one page to another. This prompted me to try and write my own script to take care of the sidebar to the left of the screen.]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s literally nothing about on the internet about using the<a href="http://jquery.com"> jQuery</a> <a href="http://docs.jquery.com/Events/toggle">toogle()</a> function on multiple collapsable panels and saving their states with cookies from one page to another. This prompted me to try and write my own script to take care of <span style="color: #c7d0cf;">the sidebar to the left of the window</span>. All the separate panels are &#8216;widgets&#8217; in Wordpress and are loaded into an &#8216;li&#8217; tag with a class name of &#8216;widget&#8217; making it simple to target the items. I&#8217;d also like to say this isn&#8217;t an Accordion as such and even the scripts for them don&#8217;t deal with unique cookies!</p>
<h3>The collapsing effect</h3>
<p>Collapsing the panels is the easy bit and just like every other &#8216;accordion&#8217; and &#8216;collapsing panel&#8217; plug-in can be achived with a simple toogle() function to slide the &#8216;ul&#8217; inside each of the widgets when the &#8216;h2&#8242; is clicked. The jQuery for that would look like this&#8230;</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Notice how we use <span style="color: #c7d0cf;">.next(&#8220;ul&#8221;)</span> to target the &#8216;ul&#8217; as it immediately precedes the &#8216;h2&#8242; which is the trigger for the effect. So far so good!</p>
<p>Next we need to add an &#8216;active&#8217; class to the &#8216;h2&#8242; on DOM ready so the user can see the state of the panels when the page loads, hopefully prompting them they can close it. When they click it we just remove the active &#8216;class&#8217;. This is really easy too by adding this&#8230;.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Add the class on page load.</span><br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Remove the class 'active' on slideUp</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Add the class 'active' on slideDown</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Here&#8217;s the CSS for the state change&#8230;.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">li.<span style="color: #660066;">widget</span> h2 <span style="color: #009900;">&#123;</span><br />
background<span style="color: #339933;">:</span>transparent url<span style="color: #009900;">&#40;</span>images<span style="color: #339933;">/</span>yourImage.<span style="color: #660066;">jpg</span><span style="color: #009900;">&#41;</span> no<span style="color: #339933;">-</span>repeat <span style="color: #000066;">scroll</span> right top<span style="color: #339933;">;</span><br />
<span style="color: #009966; font-style: italic;">/* various styling */</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
li.<span style="color: #660066;">widget</span> h2.<span style="color: #660066;">active</span> <span style="color: #009900;">&#123;</span><br />
background<span style="color: #339933;">-</span>position<span style="color: #339933;">:</span>right bottom<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Pretty standard stuff! Now comes the interesting bits!</p>
<h3>Baking the cookies!</h3>
<p>First off I downloaded <a href="http://plugins.jquery.com/project/cookie">jQuery.cookies.js</a>. This is a great jQuery plug-in for dealing with cookie operations. The basics of it are&#8230;.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'COOKIE_NAME'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'COOKIE_VALUE'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
<span style="color: #006600; font-style: italic;">//Make a cookie</span><br />
<br />
$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'COOKIE_NAME'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'null'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">//Delete the cookie</span><br />
<br />
$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'COOKIE_NAME'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">//Get the cookie. Returns cookie value.</span></div></div>
<p>The problem is that all the tutorials for collapsing panels with cookies only dealt with one panel!..and one cookie! But I have multiple panels that need to have their states saved in multiple individual cookies! As Clay Davis from &#8216;The Wire&#8217; would say,&#8221;<a href="http://uk.youtube.com/watch?v=JeaaFTNh3xY">SSSHHHIIIIIIIIIIIIIIIT</a>!&#8221;.</p>
<h3>Making unique cookies</h3>
<p>So&#8230;first I needed to find the index of the widget I&#8217;d collapsed and store it&#8217;s state in an it&#8217;s own unique cookie. Finding the index or number of the widget can be done with this line&#8230;</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Find the index of the widget. Starts at 0.</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Find the index of the widget. Starts at 0.</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Clicking the first widget 'h2' trigger will store 0 in the variable 'num'.</span></div></div>
<p>Now we can create some variables to store unique cookie names and values. Here&#8217;s how we do that&#8230;.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #006600; font-style: italic;">// Returns ----&gt; panel0, panel1, panel2 &nbsp;etc....</span><br />
<span style="color: #003366; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span> &nbsp; <br />
<span style="color: #006600; font-style: italic;">// Returns ----&gt; closed0, closed1, closed2 &nbsp;etc.... </span><br />
<span style="color: #339933;">&lt;/</span>code<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>pre<span style="color: #339933;">&gt;</span><br />
<br />
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>Create the cookies<span style="color: #339933;">&lt;/</span>h3<span style="color: #339933;">&gt;</span><br />
<br />
Now <span style="color: #000066; font-weight: bold;">for</span> every widget clicked we can create a unique cookie <span style="color: #000066; font-weight: bold;">with</span> a unique value. <br />
<span style="color: #660066;">Let</span><span style="color: #3366CC;">'s see how we set up the cookie...<br />
This creates a cookie for the first widget when it'</span>s clicked.<br />
<br />
<span style="color: #009900;">&#91;</span>cc lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Find index of 0.</span><br />
&nbsp; &nbsp; <br />
$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Slide up panel</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Remove 'active' class</span><br />
&nbsp; &nbsp; <br />
<span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span> &nbsp;<span style="color: #006600; font-style: italic;">// Returns ----&gt; panel0</span><br />
<span style="color: #003366; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span> &nbsp; <span style="color: #006600; font-style: italic;">// Returns ----&gt; closed0 </span><br />
&nbsp; &nbsp; <br />
$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> cookieValue<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
<span style="color: #006600; font-style: italic;">// Cookie is stored as panel0 with a value of closed0.</span></div></div>
<h3>Deleting the cookies</h3>
<p>To delete the cookie when widget one is clicked or &#8216;toogled&#8217; we do this&#8230;..</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Find index of 0.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Slide down panel</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <span style="color: #006600; font-style: italic;">// Add 'active' class &nbsp; &nbsp; </span><br />
&nbsp; &nbsp; <br />
<span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Returns ----&gt; panel0</span><br />
&nbsp; &nbsp; <br />
$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <br />
<span style="color: #006600; font-style: italic;">// Cookie with the name panel0 is deleted.</span></div></div>
<p>The whole code for this looks like this&#8230;..</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2.active&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> cookieValue<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Getting the cookie values</h3>
<p>Now we have to retrieve the cookies and their values to set the state of the widgets when the page is reloaded. This is easily achived using a <span style="color: #c7d0cf;">for</span> loop with an <span style="color: #c7d0cf;">if</span> statement inside it.</p>
<p>First were going to add a line to see how many widgets we have so we know how long the loop should be. I&#8217;ve also created a variable to define the &#8216;ul&#8217; as &#8216;panel&#8217;. If you have nested lists you may want to add <span style="color: #c7d0cf;">:first</span> to the selector.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li.widget h2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> panel <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget ul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>I&#8217;ve done this for any widget that has a title or &#8216;h2&#8242; tag. Some widgets like &#8216;text&#8217; or &#8216;RSS&#8217; widgets you may want to leave out. This is optional&#8230;.</p>
<p>Heres the loop with some comments. c=0 is important it&#8217;s the loop index.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>c<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>c<span style="color: #339933;">&lt;=</span>l<span style="color: #339933;">;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #006600; font-style: italic;">// Loop up to the number of widgets. l is the length.</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> cvalue <span style="color: #339933;">=</span> $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
<span style="color: #006600; font-style: italic;">// Store the value of found cookie in cvalue while looping.</span><br />
&nbsp; <br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> cvalue <span style="color: #339933;">==</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> c <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp;<br />
<span style="color: #006600; font-style: italic;">// If values match do this... </span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>panel<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>display<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Hide the ul if cookie is found for element.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>panel<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'inactive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// Change state of effect trigger.</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>If the loop finds a cookie with the name &#8216;panel0&#8242; it will return a value of &#8216;closed0&#8242; which tells the if statement to hide the widget &#8216;ul&#8217; with an index of 0 and remove the &#8216;active&#8217; class and add a class of &#8216;inactive&#8217; on page load.</p>
<p>When the page loads any widget that has an &#8216;h2&#8242; with the class &#8216;inactive&#8217; will be hidden as it doesn&#8217;t have the class  &#8216;active&#8217;&#8230;&#8230;.Great!</p>
<h3>Major Problem!!!</h3>
<p>&#8230;..or not! Just like Microsoft there is one massive flaw in this code!!! I&#8217;d like to add that up to here took about an hour, what follows took a whole day!</p>
<p>The toogle() function works on the premise that the first part of the code (the first &#8216;click&#8217; function) will slide the element UP and the second part (the second &#8216;click&#8217; on the element) will slide the element DOWN. Well what if the element is already UP on page load. Clicking the trigger returns nothing as the element is already UP&#8230;?!? You need to click it again to get it to slide down which from a UI point of veiw it pretty&#8230;well, crap!</p>
<p>I tried so many different things from &#8216;if else&#8217; statements to &#8216;click&#8217; functions, the list goes on. It seemed if one thing worked something else wouldn&#8217;t. It was like a dream that you&#8217;re chasing something but can never catch up with it. At one point I thought I was losing my mind!</p>
<p>The answer turned out to be really simple in the end (Doh!) an lay in something I&#8217;d added to the code but never thought of using&#8230; the &#8216;inactive&#8217; class. I discovered that if I duplicated the toggle() function and reversed the functions so the first click performed a DOWN slide and assigned that to <span style="color: #c7d0cf;">$(&#8216;li.widget h2.inactive&#8217;)</span> it produced the desired result. Not very elegant but then neither am I!</p>
<p>The whole thing looks like this&#8230;.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li.widget h2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> panel <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget ul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>c<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>c<span style="color: #339933;">&lt;=</span>l<span style="color: #339933;">;</span>c<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> cvalue <span style="color: #339933;">=</span> $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> cvalue <span style="color: #339933;">==</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> c <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>panel<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>display<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>panel<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'inactive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2.active&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> cookieValue<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2.inactive&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;active&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'inactive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> num <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li.widget h2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'panel'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> <span style="color: #3366CC;">'closed'</span> <span style="color: #339933;">+</span> num<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #339933;">,</span> cookieValue<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">,</span> expires<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Conclusion</h3>
<p>Multiple collapsable panels with cookie states for every unique panel. Awesome!!!</p>
<p>I hope somebody finds this useful and is spared the brain ache I went through. To coolest thing is as the demo relies on Wordpress&#8217; markup you can drop it straight into a theme your developing. I&#8217;m going to turn it into a plugin soon too (once I&#8217;ve figured out how to do that!). I&#8217;d love to hear if any knows of a better and more elegant way of doing this, so leave a comment if you do!</p>
<p>For a demo, look no further than the sidebar menu at the top of this page. Close some widgets and reload the page!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tobypitman.com/multiple-collapsable-panels-with-cookies/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>JQuery: Fade elements sequencially without chaining</title>
		<link>http://www.tobypitman.com/jquery-fade-elements-sequencially-without-chaining/</link>
		<comments>http://www.tobypitman.com/jquery-fade-elements-sequencially-without-chaining/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 20:07:13 +0000</pubDate>
		<dc:creator>Toby</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.tobypitman.com/?p=98</guid>
		<description><![CDATA[Here's a useful trick for fading similar elements sequencially without having to chain the effects. In the first example I have 5 divs that I want to fade in one after the other. Instead of giving them all unique ID's and chaining the fade effects like this....]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a useful trick for fading similar elements sequencially without having to chain the effects. In the first example I have 5 divs that I want to fade in one after the other. Instead of giving them all unique ID&#8217;s and chaining the fade effects like this&#8230;.<br />
<br/></p>
<pre>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$(&quot;#box1&quot;).fadeIn(&quot;slow&quot;, function(){<br />
&nbsp; $(&quot;#box2&quot;).fadeIn(&quot;slow&quot;, function(){<br />
&nbsp; &nbsp; $(&quot;#box3&quot;).fadeIn(&quot;slow&quot;, function(){<br />
&nbsp; &nbsp; &nbsp; $(&quot;#box4&quot;).fadeIn(&quot;slow&quot;, function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; $(&quot;#box5&quot;).fadeIn(&quot;slow&quot;);<br />
&nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; &nbsp;});<br />
&nbsp; &nbsp;});<br />
});</div></div>
</pre>
<p><br/><br />
&#8230;&#8230; you can instead give them a class of &#8216;box&#8217; and find the amount using <span style=color:#fff;>.length;</span> and use an <span style=color:#fff;>if</span> statement to loop through the until all the relevant elements are visible using a callback function.<br />
<br/></p>
<pre>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var i = 0<br />
var n = $(&quot;.box&quot;).length;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
function showbox() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; if(i &lt;= n){ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; $(&quot;.box&quot;).eq(i).fadeIn(600, function(){ i++; showbox(); });<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; } showbox();</div></div>
</pre>
<p><br/><br />
It&#8217;s a lot more efficient code wise and produces the same effect. Here&#8217;s the same thing but using a &#8216;click&#8217; event to trigger the function.<br />
<br/></p>
<pre>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var j = 0<br />
var a = $(&quot;.list&quot;).length;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; $(&quot;#trigger&quot;).click(function showlist(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; if(j &lt;= a){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; $(&quot;.list&quot;).eq(j).fadeIn(200, function(){ j++; showlist(); });<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; });</div></div>
</pre>
<p><br/><br />
Might be a nice effect for a menu? I was going to use it to fade in the posts on the blog homepage but IE7 can&#8217;t render one transparent PNG over another when faded. It comes out looking like a jaged GIF with the top PNG&#8217;s transparent pixel looking black instead. Once again IE ruins everything!!!</p>
<p><a class="viewdemo" href="http://development.tobypitman.com/jquery/sequence.html" title="">View Demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tobypitman.com/jquery-fade-elements-sequencially-without-chaining/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
