Simple JQuery Accordion menu - Redux

Thursday May 8 2008

This post is here for historical / archival purposes. Comments are disabled. Please visit the link below for the latest version of the JQuery accordion.

Yup, yet another JQuery Accordion Plugin

A few months ago I blogged about a simple JQuery accordion menu. This entry became wildly popular and received a lot of feedback in comments. A fair amount of interesting questions came up and I received a lot of requests for changes / enhancements both in comments and in private email. Some people like it to function as an while others want a simple sliding menu. Then there were people who needed more than one instance on the page. And there was more. I decided to take a second look at this menu in an attempt to make it more flexible and versatile. I'm hereby posting the results of my efforts.

One menu to rule them all. Let's take a look!

Dark Matter Pro: a premium photoblog template.

Affiliate program available

Multiple instances of the menu

The first version of my menu did not allow more than one instance of it on the same page. A fair amount of people turned out to need this. The new menu allows as many instances as you want! Just like in the original menu, the markup consists of a set of nested unordered lists. For the new version of the menu to work, all you need to do is:

  • Give each menu a unique ID
  • Give each menu a class named menu

This is all you need to make the new script 'enable' your menus.

Configurability

Besides allowing multiple instances of the menu, the new version allows different behaviours, configurable by simply adding CSS classes. All these are demonstrated on the demo page. Let's take a look at what they do:

expandfirst

If you want the first menu item to be expanded at page load, simply add an extra CSS class named expandfirst besides the mandatory class menu. Without the expandfirst class, the entire menu will be collapsed by default.

Non-accordion (standard expanding / collapsing menu)

Don't want an accordion but just a simple expanding / collapsing menu? Add the CSS class named noaccordion and the menu will not function as an accordion.

Collapsible accordion

Want the accordion functionality but the ability to fully collapse? Use the CSS class named collapsible. When one item is open, the menu will work like an accordion. When clicking the open item it will collapse.

The class name expandfirst can be combined with noaccordion and collapsible for even more flexibility.

Closing notes

Here it is. All this in just 29 lines of powered . I absolutely love JQuery and it's amazing power. I hope you will enjoy this snippet.

The script has been tested in Firefox, Safari and Internet Explorer. If you find any issues in other browsers, let me know!

Source

  1. function initMenus() {
  2. $('ul.menu ul').hide();
  3. $.each($('ul.menu'), function(){
  4. $('#' + this.id + '.expandfirst ul:first').show();
  5. });
  6. $('ul.menu li a').click(
  7. function() {
  8. var checkElement = $(this).next();
  9. var parent = this.parentNode.parentNode.id;
  10. if($('#' + parent).hasClass('noaccordion')) {
  11. $(this).next().slideToggle('normal');
  12. return false;
  13. }
  14. if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
  15. if($('#' + parent).hasClass('collapsible')) {
  16. $('#' + parent + ' ul:visible').slideUp('normal');
  17. }
  18. return false;
  19. }
  20. if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
  21. $('#' + parent + ' ul:visible').slideUp('normal');
  22. checkElement.slideDown('normal');
  23. return false;
  24. }
  25. }
  26. );
  27. }
  28. $(document).ready(function() {initMenus();});

Go to the demo page to take a look and download it if you like it.

small update: I have changed the menu somewhat so that it remembers state with a cookie. It uses the JQuery Cookie Plugin. I have also fixed the bug that Chris mentioned in the comments. You can check out the 'smart' version on this page.

Even though I'm not sure how much it would appeal to hardcore JQuery fans, but nevertheless:

My YUI Accordion Widget can do just about everything people have asked for in the comments here, and a lot more! You may want to check it out!

If you want to use this commercially, just go ahead. It's better than all these people emailing me about compensation and then never get back to me. A donation is highly appreciated (at paypal@i-marco.nl) but don't feel obliged. But please, don't write me email promising one when you aren't gonna actually do it. Sorry if this sounds harsh but I'm really disappointed in how people dealt with this.
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.

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