<?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>the director. &#187; Coding</title>
	<atom:link href="http://www.mikecentola.com/blog/category/technology/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikecentola.com/blog</link>
	<description>[ mike centola's blog ]</description>
	<lastBuildDate>Tue, 11 Jan 2011 05:45:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Saving state with the jQuery Accordion Menu</title>
		<link>http://www.mikecentola.com/blog/2009/05/31/saving-state-with-the-jquery-accordion-menu/</link>
		<comments>http://www.mikecentola.com/blog/2009/05/31/saving-state-with-the-jquery-accordion-menu/#comments</comments>
		<pubDate>Sun, 31 May 2009 13:44:54 +0000</pubDate>
		<dc:creator>mikecentola</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[accordion]]></category>
		<category><![CDATA[animated]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[menu]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[slide]]></category>
		<category><![CDATA[true negative]]></category>
		<category><![CDATA[user interface]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.mikecentola.com/blog/?p=176</guid>
		<description><![CDATA[I have been working on the new True Negative website pretty much non-stop lately and I'm using several differernt things from the jQuery JavaScript library. One of these is the jQuery Accordion Menu, from their User Interface library. It's a great simple menu that can be implemented with very little javascript code. One thing that [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working on the new <a title="True Negative" href="http://www.truenegative.com" target="_blank">True Negative</a> website pretty much non-stop lately and I'm using several differernt things from the <a title="jQuery" href="http://www.jquery.com" target="_blank">jQuery</a> JavaScript library. One of these is the<a title="jQuery Accordion Menu" href="http://docs.jquery.com/UI/Accordion" target="_blank"> jQuery Accordion Menu</a>, from their <a title="jQuery UI" href="http://ui.jquery.com" target="_blank">User Interface</a> library. It's a great simple menu that can be implemented with very little javascript code.</p>
<p>One thing that it doesn't inherently support is saving the state between webpages, which is something I had wanted since I'm using it as a menu, not just for content display. Functionally, I knew what had to be done in order to get it to save state. It was obvious that I needed to use cookies and some quick javascript to save what menu was open. I then came across the <a title="jQuery Cookie Plugin" href="http://plugins.jquery.com/project/cookie" target="_blank">jQuery Cookie plugin</a>, which really simplifies working with cookies in jQuery.</p>
<p>Setting a cookie with the plugin is as simple as follows:</p>
<blockquote>
<pre>$.cookie('cookiename', 'cookiecontents');</pre>
</blockquote>
<p>The accordion menu has an event change attribute, which can be set to a function or just inline code. Using both this feature and the cookie plugin, it was very simple to write the code that I needed in order to get the result I wanted. Here is the code and I will explain it below:</p>
<blockquote>
<pre>// accordion menu
$('#menu').accordion({
	header: 'h3',
	navigation: true,
	active: '.selected',
	autoHeight: false,
	clearStyle: true,
	collapsible: true,
	alwaysOpen: false,
	animated: 'slide',
	change: function(event,ui) {
		var hid = ui.newHeader.children('a').attr('id');
	if (hid === undefined) {
		$.cookie('menustate', null);
	} else {
		$.cookie('menustate', hid, { expires: 2 });
	}
}
});

// check cookie for accordion state
if($.cookie('menustate')) {
	$('#menu').accordion('option', 'animated', false);
	$('#menu').accordion('activate', $('#' + $.cookie('menustate')).parent('h3'));
	$('#menu').accordion('option', 'animated', 'slide');
}</pre>
</blockquote>
<p>The first section is pretty straightforward. It defines and applies the accordion menu to a div tag with an id of menu. All of the various options are laid out in the <a title="jQuery Accordion Docs" href="http://docs.jquery.com/UI/Accordion" target="_blank">Accordion docs</a> and I just set some of the standard ones in order to get the look and feel that I wanted. The important part is the change option:</p>
<blockquote>
<pre>change: function(event,ui) {
		var hid = ui.newHeader.children('a').attr('id');
	if (hid === undefined) {
		$.cookie('menustate', null);
	} else {
		$.cookie('menustate', hid, { expires: 2 });
	}</pre>
</blockquote>
<p>Here you can see that I check the value of the menu that is open to see if it is undefined, which would mean the menu is closed. If it is open, it will have a value in there, so I store that in a cookie called "menustate" for use when the page is reloaded.</p>
<blockquote>
<pre>// check cookie for accordion state
if($.cookie('menustate')) {
         $('#menu').accordion('option', 'animated', false);
         $('#menu').accordion('activate', $('#' + $.cookie('menustate')).parent('h3'));
         $('#menu').accordion('option', 'animated', 'slide');</pre>
</blockquote>
<p>Upon page reload I have it check the "menustate" cookie to see if it has been set or not. If it is, I first turn off the animation so the visitor does not see it open again, then I call the activate method to open the one with the id that is stored in the cookie. Lastly, I reenable the animation. It works great!</p>
<p>Unfortunately, I have no completed the True Negative site yet, so I do not have an example of this working. As soon as I finish the site, I'll update this blog post with a link to it. Hope this helps anyone trying to do this in a simple way. Thanks for reading!<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikecentola.com/blog/2009/05/31/saving-state-with-the-jquery-accordion-menu/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

