JQuery Accordion menu
This post is here for historical / archival purposes. Comments are disabled. Please visit the link below for the latest version of the JQuery accordion.
This weekend I did a simple Accordion menu using JQuery. I figured it would be nice to share it on my blog as it may be useful to other people. I have no idea if this has already been done, if someone else has done a better job at it but... this one is mine! Feel free to use it if you need an unobtrusive Javascript accordion component on your (non commercial) website.
I whipped up a little demo page where you can see the menu in action and download the source. The menu will open with the first submenu expanded and items that don't contain a submenu will behave like regular links.
I didn't think I'd need to make this remark but: please don't be an asshole and do what these people from html.it did. Not with this, not with any of my other stuff, heck, not with anyone's stuff. It's just wrong. Thanks!
Source
function initMenu() {$('#pagemenu ul').hide();$('#pagemenu ul:first').show();$('#pagemenu li a').click(function() {var checkElement = $(this).next();if((checkElement.is('ul')) && (checkElement.is(':visible'))) {return false;}if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {$('#pagemenu ul:visible').slideUp('normal');checkElement.slideDown('normal');return false;}});}$(document).ready(function() {initMenu();});
Go to the demo page to take a look and download it if you like it.
small update: I added a version that acts as a simple collapsing menu. Even less code needed for that:
function initMenu() {$('#menu ul').hide();$('#menu li a').click(function() {$(this).next().slideToggle('normal');});}$(document).ready(function() {initMenu();});
Safari 3 weirdness update
In the comments a problem with Safari was described. I just tried the example page in the latest nightly build of Webkit and it's working fine. I guess it's therefore safe to assume we're dealing with a Safari 3 bug.
Filed under: programming
Number of comments:
Number of trackbacks:
Tagged with: 






Last week I posted about 
