Yup, Yet another JQuery Accordion Plugin

Saturday Feb 27 2010

Over two years after posting this simple JQuery accordion menu and later on an improved JQuery accordion menu I'm still receiving epic amounts of traffic on these posts. New comments keep getting posted every week as well. When I posted these I never even dreamed they would draw this much attention and they were never intended to be more than just quick code snippets. Apparently I was wrong. JQuery is going extremely strong and somehow it seems I managed to fill some sort of void with my little accordion code snippet.

Questions are being asked, requests are made for things the snippet won't do without modifications and most importantly, problems are being reported with newer versions of JQuery. Because of all this I decided to once again do a new version of this little JQuery accordion. I tried to address most of the issues reported by readers and add some new features. Let's take a look at version 3, now in JQuery plugin flavor.

UPDATE: fixed 2 bugs reported in the comments. See my comment for details.

UPDATE 2: (April 11, 2010): fixed a bug causing erratic behavior in nested accordions.

Dark Matter Pro: a premium photoblog template.

Affiliate program available

Get the code

You can view the code for the JQuery accordion plugin, look at demos and download the sourcecode here.

New Features

For those who already know the previous versions I'll start with new things this version can do that the old one can't:

  • Nested accordions. Yep, they work. It's now possible to have an accordion inside an accordion item without the two disrupting eachother. My advice was and still is: use with caution. If not styled right this can be extremely confusing for your users.
  • Panels that hold other things than lists. You can now add anything. This is handy for those 'Apple style' accordions with content in each panel.
  • An 'active' class is now added to each toggle that has an open panel below it. Nice to provide an extra visual cue to your users.
  • Built according to the JQuery Plugin pattern
  • No more need for menus to have an ID
  • Any panel can be opened at page load time by using the class name expand on the list item that holds the toggle + panel you want to be opened

Cool features that were already there:

  • Collapsible accordion: only one panel can be open at any given time but this one panel can be collapsed in order for the entire accordion to be collapsible
  • Can function as a regular set of toggle panels where any panel can be open or closed

How to use the JQuery accordion plugin

Create markup that looks like this:

  1. <ul class="menu">
  2. <li>
  3. <a href="#">Item 1</a>
  4. <ul class="acitem">
  5. <li>...</li>
  6. <li>...</li>
  7. <li>...</li>
  8. </ul>
  9. </li>
  10. <li>
  11. <a href="#">Item 2</a>
  12. <ul class="acitem">
  13. <li>...</li>
  14. <li>...</li>
  15. <li>...</li>
  16. <li>...</li>
  17. </ul>
  18. </li>
  19. </ul>

As you can see, the main UL element containing the entire accordion needs to have menu as one of it's class names. Secondly, each item that serves as a toggle panel needs to have the class name acitem. The element with class="acitem" can be any element. It doesn't need to be a list of links. You can use a DIV element containing any kind of content if you like.

Expanding items at page load time

In order to have any item expanded as the page loads, add the class name expand to the panel you want to be expanded. For example:

  1. <ul class="menu">
  2. <li class="expand">
  3. <a href="#">Item 1</a>
  4. <ul class="acitem">
  5. <li>...</li>
  6. ...
  7. ...

Keeping state

In the comments below the previous version of this plugin, various people were asking for a way to maintain state. I even made a (messy) attempt to do this in Javascript but... I think it's wrong. It's much better (and easier) to maintain state in the code that controls your page markup. Now that you can have any panel expanded at page load time by means of the expand class shown in the above example you can control the state of your accordions by having your page generation code set this class. Nice, easy and no cookie crumbs all over the floor.

Wrapping it up

Like I mentioned at the start of this post, JQuery is going stronger than ever. As much as I love YUI and the accordion widget I built with it there's simply way more interest in the JQuery one. And that's why I just had to do an update to it.

CSS

Important note: The CSS in on the example page is not perfect and certainly not pretty. It's strictly intended for demonstration purposes. This also means I didn't vigorously make it perfect for all browsers. You're strongly encouraged to use your own styling for your accordion menus.

DEMO AND SOURCE CODE

bookmarking

Commentary

Join the discussion! Leave a comment through the comment form below!

Got something to add to this?

Feel free to leave a comment on this site. You can use Textile and Emoticons. Your email address is only used to show a gravatar. Please stay on-topic and use common decency. Spammers will be shot in front of a live studio audience.

If you plan on posting code, use pastebin please and post a URL to the code. The comment processing doesn't deal very well with code. Sorry for the inconvenience.

Human comment spammers: don't bother posting your crap here. Comments are moderated and I won't let any of your shit through.

Remember personal info?
Yes
No

Trackbacks

If you have an interesting related post on your own site you can leave a trackback. As they say: 'a little AJAX a day keeps the spammers away' which is why you'll have to click below to generate a trackback key. The key will be valid for 15 minutes and can be used only once.

 

  • Featured Links
RockySomewhere near the Orion NebulaBookalicio.usGolden Gate BridgeThames River BankJackie and mePimpin' it