Recent Posts
- (02/24) Calling an Element from a Helper TAGS:CakephpWeb ProgrammingUsageTutorialExample
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
Subscribe to my feed
MainelyDesign.com Blog
Jquery Accordion Menu - Expanding UL Menu
Posted on 11/17/2009 at 01:25 pm by Kevin Wentworth
Viewed 11,116 times | 2 comments
I looked around and found some interesting examples and existing plugins for creating an accordion menu in Jquery. What I was looking for was a nested unordered list (UL) that would collapse and expand based on which menu item was clicked. If there wasn't a child UL, then the menu link should be clickable, instead of an accordion. Sounds simple right?
Semantic HTML Markup
I wanted to keep my HTML clean, and for me that means using semantically correct UL/LI for presenting the menu/navigation options. The jqueryui accordion plugin wants you to use <h3> and <div> (although I think it is configurable). I actually had an issue getting jqueryui to work, so I abandoned it as an option for my jquery accordion menu. Here's what my HTML looks like:
- <div id="sideNav">
- <ul>
- <li><a href="/section.php">Section 1</a>
- <ul>
- <li><a href="/link.php">Link 1</a></li>
- <li><a href="/link.php">Link 2</a></li>
- <li><a href="/link.php">Link 3</a></li>
- <li><a href="/link.php">Link 4</a></li>
- </ul>
- </li>
- <li><a href="/section.php">Section 2</a>
- <ul>
- <li><a href="/link.php">Link 5</a></li>
- <li><a href="/link.php">Link 6</a></li>
- </ul>
- </li>
- <li><a href="/section.php">Section 3</a></li>
- <li><a href="/section.php">Section 4</a></li>
- <li><a href="/section.php">Section 5</a></li>
- </ul>
- </div>
The JQuery Accordion Javascript Code
In order to make the accordion work, I took the unobtrusive approach, meaning the entire menu will show if javascript is disabled, but if it's enabled all li's that have a child ul will be hidden. Also, if the class of the link (in the expanding ul) is set to current, that accordion will be expanded by default. There is no customization, plugin, etc. so please use as you see fit.
- $(document).ready( function() {
- $('div#sideNav li > ul').hide(); //hide all nested ul's
- $('div#sideNav li > ul li a[class=current]').parents('ul').show().prev('a').addClass('accordionExpanded'); //show the ul if it has a current link in it (current page/section should be shown expanded)
- $('div#sideNav li:has(ul)').addClass('accordion'); //so we can style plus/minus icons
- $('div#sideNav li:has(ul) > a').click(function() {
- $(this).toggleClass('accordionExpanded'); //for CSS bgimage, but only on first a (sub li>a's don't need the class)
- $(this).parent().siblings('li').children('ul:visible').slideUp('fast')
- .parent('li').find('a').removeClass('accordionExpanded');
- return false;
- });
- });
The CSS for Making the Accordion Menu Pretty
Not much CSS is needed. The selector hooks are pretty straightforward. After the jquery runs, the li's that contain an accordion menu will have a class of accordion. If the accordion is expanded the li > a will have a class of accordionExpanded. Here's a snippet of my CSS for making the plus/minus icons:
- div#sideNav li.accordion a {
- background-image: url(../img/template/plus-minus.gif);
- background-repeat: no-repeat;
- background-position: top right;
- padding-right: 20px;
- }
- div#sideNav li.accordion a.accordionExpanded {
- background-position: 100% -15px;
- }
- div#sideNav li.accordion li a {
- background-image: none; /* undo above for sub links */
- padding-right: 0px;
- }
All you have to do is make styles for the li.accordion and a.accordionExpanded. I switched out the background image. Here's how mine turned out. Once the site is live, I'll post the link to it.

BTW, tested in IE7, IE8 and FF3.
Cheers,
-Kevin Wentworth
Tags for Jquery Accordion Menu - Expanding UL Menu
Jquery | Example | Web Design | Css
Comments for this Posting
Posted by Cory
on 23/5/11
Vous avez de bons points il, c'est pourquoi j'aime toujours verifier votre blog, Il semble que vous etes un expert dans ce domaine. maintenir le bon travail, Mon ami recommander votre blog.
Mon francais n'est pas tres bon, je suis de l'Allemagne.
Mon blog:
Meilleur Taux aussi simulateur Rachat De Credit
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!

Posted by hydrogen generator china
on 17/2/11
Great stuff my friend!Impressive post, a great deal of useful knowledge. I have been looking for such some info for some time and i got it here, thanks......
http://www.hydrogen-generator.biz