Last week I had to create an accordion style menu for a project. Accordion menus are a very, very handy tool to show and hide information as needed. I have used them on sites like http://www.spokanesymphony.org and http://2008-2009.spokanesymphony.org. While the accordion system I used on these sites worked well, I decided to write my own using jQuery with the goal of making it as simple as possible.
I have found many of the existing accordion menu systems to be bloated and difficult to understand. I knew there had to be an easier way to create an accordion menu. Instead of using a list like most alternate systems, this system uses a grouping of DIV elements with specific names.
ORIGINAL VERSION: DEMO | DOWNLOAD
UPDATED VERSION: DEMO | DOWNLOAD *
* Note: The updated version was released on 3/25/10 and it is slightly different than the example detailed in this post, however, installation is virtually the same. The updated version addresses a number of frequent feature requests from fans of the script. These include use of jQuery 1.4.2, adding a mouseover effect, adding a selected effect, and the ability to close open menus by clicking their header.
Note: Here is an example of how to pin open the first item and an example of how to select any item to pin open on load.
How to implement this jQuery accordion style menu:
Step 1 – Include jQuery:
For a jQuery beginner the first thing you need to do is to include the jQuery library. Google hosts the library so you can link to it there. This can be done by putting the following code in the head of your document:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> </script> |
Step 2: – Create the JavaScript
Next you need to create a blank JavaScript file. For the example, this file is called javascript.js. This file will contain the jQuery code required for the menu. Once you save the file include it in the head of your document with the proper path just as we did the jQuery library in Step 1. An example of the JavaScript code to include in this document is shown below:
$(document).ready(function() { //ACCORDION BUTTON ACTION $('div.accordionButton').click(function() { $('div.accordionContent').slideUp('normal'); $(this).next().slideDown('normal'); }); //HIDE THE DIVS ON PAGE LOAD $("div.accordionContent").hide(); }); |
Step 3 – Create the CSS:
Now we need to create a CSS document with our two selectors accordionButton and accordionContent and then include that in the head of the document. Examples of these selectors are included below. The color, contents, and visual formatting can all be manipulated. It is important these elements retain their float: left or display: inline-block property so that they will display inline.
#wrapper { width: 800px; margin-left: auto; margin-right: auto; } .accordionButton { width: 800px; float: left; background: #003366; border-bottom: 1px solid #FFFFFF; cursor: pointer; } .accordionContent { width: 800px; float: left; background: #95B1CE; display: none; } |
Step 4 – Create the HTML:
Now we need to create the HTML we’re going to manipulate. For this example we will keep the page very, very simple. In a more complicated HTML document you need to make sure the two classes used for the accordion DIV items are not used elsewhere. jQuery will be looking for the class of the element so if it is present elsewhere in the HTML document it will cause some unexpected problems. The full HTML document also displaying the includes is shown below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>jQuery Accordion Style DIV Menu</title> <link href="style/format.css" rel="stylesheet" type="text/css" /> <link href="style/text.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> </script> <script type="text/javascript" src="includes/javascript.js"> </script> </head> <body> <div id="wrapper"> <div class="accordionButton">Button 1 Content</div> <div class="accordionContent">Content 1<br /><br /><br /><br /><br /><br /><br /><br />Long Example</div> <div class="accordionButton">Button 2 Content</div> <div class="accordionContent">Content 2<br /><br /><br /><br /><br />Medium Example</div> <div class="accordionButton">Button 3 Content</div> <div class="accordionContent">Content 1<br />Short Example</div> <div class="accordionButton">Button 4 Content</div> <div class="accordionContent">Content 4<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />Extra Long Example</div> </div> </body> </html> |
Notice that we have alternating rows with the styles accrodionButton and accordionContent. Clicking accordionButton will trigger the drop of the next instance of accordionContent. It is very important these elements alternate properly with nothing inbetween for the system to function properly. Each element can contain HTML, nested divs, or other styling elements.
Step 5 – Check out the Result!:
Now we have all of our code we need. View the system in the browser and it should work great. Clicking on each button will cause the content below it to drop. The content will retract if another button is clicked. (Note: If you needed to keep the content elements open after a click on a seperate button instead of having them retract you can do this easily by removing this line from the javascript.js include:
$('div.accordionContent').slideUp('normal'); |
View a complete example or download a complete example:
Click here to view the demo of the accordion style jQuery menu
Click here to download the demo files for the jQuery accordion style menu
Is there any way that I can have the top button drop and open all the content and have the balance of the buttons closed? Id like to have each page I use this to show content of the top (or a selected button) and then allow the user to manually choose other buttons for other content.
Thanks much
[...] Stupid Simple jQuery Accordion Menu [Link] [...]
Incredibly simple. You’re a lifesaver.
I really like the simplicity, I just added it to my project.
One thing though that I didn’t like was when you click on the opened div’s header and it would slide up and down once. I added a few simple lines that makes it still for those who’d like it be still.
$(document).ready(function() {
//ACCORDION BUTTON ACTION
$(‘div.accordionButton’).click(function() {
$(‘div.accordionContent’).removeClass(‘current’);
$(this).next().slideDown(‘normal’);
$(this).next().addClass(‘current’);
$(‘div.accordionContent:not(.current)’).slideUp(‘normal’);
});
//HIDE THE DIVS ON PAGE LOAD
$(“div.accordionContent”).hide();
});
Hy Ryan,
Thanks for the accordion menu. It´s really very good.
I´ve a question about usage.
In my accordion menu, the first item must be open when load the page (I got it). The others itens must be closed and the accordionButtons no-active. Inside the accordionContent I´ll put two buttons: previous step and next step. This buttons inside the accordionContent must be works like accordionButton.
One more request… I made 2 accordionButtons. If item no-active, show the accordionButton1. Else, show the accordionButton2.
Can you help me?
Thanks!
I love this plugin!
Thanks so much!
[...] http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/ (No Ratings Yet) Loading … [...]
I realize this is a few years old, put it’s great! Perfect for a beginner like me
I used your accordian for a menu and sublinks. Works great. However I want to keep the accordion submenu state opened after link is clicked? I am an extreme novice, so please explain like you would to a 5 year old. I tried using the solution you had posted for Justin on October 26, 2009 @ 2:51 pm and I must be missing something. The state stays open however it does not go to the linked page.
Here is the script:
$(document).ready(function() {
//ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
$(‘.accordionButton’).click(function() {
//REMOVE THE ON CLASS FROM ALL BUTTONS
$(‘.accordionButton’).removeClass(‘on’);
//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
$(‘.accordionContent’).slideUp(‘normal’);
//IF THE NEXT SLIDE WASN’T OPEN THEN OPEN IT
if($(this).next().is(‘:hidden’) == true) {
//ADD THE ON CLASS TO THE BUTTON
$(this).addClass(‘on’);
//OPEN THE SLIDE
$(this).next().slideDown(‘normal’);
}
});
/*** My Addition ***/
//Links click function
$(‘.links’).click(function(){
if ($(this).is(‘.activeLink’)) //already active link
{
return false;
}
else
{
$(‘.activeLink’).removeClass().addClass(‘links’);
$(this).removeClass().addClass(‘activeLink’);
return false;
}
});
//Links hover function
$(‘.links’).hover(function(){
if ($(this).is(‘.activeLink’)) //link is already active
{
return false;
}
else
{
$(‘.hoveredLink’).removeClass().addClass(‘links’);
$(this).removeClass().addClass(‘.hoveredLink’);
}
}, //and yeah! it’s not a semi-colon
function(){
if ($(this).is(‘.activeLink’)) //link is already active
{
return false;
}
else
{
$(‘.hoveredLink’).removeClass().addClass(‘links’);
}
});
//Automatic triggers
$(‘#open’).trigger(‘click’)
$(‘.links:first’).trigger(‘click’);
//closes the document.ready function
/*** END My Addition ***/
/*** REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
//ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER
$(‘.accordionButton’).mouseover(function() {
$(this).addClass(‘over’);
//ON MOUSEOUT REMOVE THE OVER CLASS
}).mouseout(function() {
$(this).removeClass(‘over’);
});
/*** END REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
/********************************************************************************************************************
CLOSES ALL S ON PAGE LOAD
********************************************************************************************************************/
$(‘.accordionContent’).hide();
});
Here is the CSS:
.menuHeader{
margin-left:10px;
width:200px;
font-size:16px;
font-weight:bold;
font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;
color:#fff;
line-height:32px;
}
.accordionButton {
float: left;
background-color:#eaeaea;
border-bottom: 1px inset #ccc;
padding-left:10px;
width: 190px;
font-size:12px;
font-weight:bold;
font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;
color:#004155;
line-height:32px;
cursor: pointer;
}
.accordionContent {
width: 200px;
float: left;
background: #fff;
padding-bottom:10px;
font-size:11px;
font-weight:bold;
color:#004155;
}
.accordionContent ul{
float: left;
padding-left:10px;
margin-left:10px;
width: 175px;
font-weight:bold;
color:#004155;
}
.accordionContent li{
list-style:url(../imgs/Bullet.png);
margin:0px 0px 0px 5px;
border-bottom:none;
line-height:20px;
color:#004155;
font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;
}
.links{
margin:0px 0px 0px 5px;
font-size:11px;
color:#004155;
font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;
}
.hoveredLink{
background:#a3a6a8 url(../imgs/SelctedArr.png) no-repeat right;
color:#fff;
font-size: 11px;
font-weight:normal;
text-decoration: none;
}
.activeLink{
background:url(../imgs/SelctedArr.png) no-repeat right;
color:#004155;
}
/***********************************************************************************************************************
EXTRA STYLES ADDED FOR MOUSEOVER / ACTIVE EVENTS
************************************************************************************************************************/
.on {
background:#ffffff;
border:none;
}
.over {
background:#a3a6a8;
}
Here is the HTML:
Resources
Information Center
How It Works
FAQ’s
Videos
Help
Trust Center
Our Guarantee
Determining Your Offer
Payment/Trade-In Centers
Please help me out.
Thank you!
I see HTML does not show up on this. Very inconvient.
I hope you can help even if the HTML is not showing.
Thank you,
Romona
Question, have a client using wix as a site builder (HTML5 version) Is there a way to implement this within an html widget (iframe)?
Any possibility to add a sub-accordion, that is an accordion within the accordion like so:
Main Accordion Heading1 – Collapse/Expand list of Names
Name1 – Collapse/Expand content
Name2 – Collapse/Expand content
Name3 – Collapse/Expand content
Main Accordion Heading2 – Collapse/Expand list of names
Name4 – Collapse/Expand content
Name5 – Collapse/Expand content
Name6 – Collapse/Expand content
If it’s possible please give me an example of the coding as I’m new to all this.
Thanks for your help
David
Can someone please help me implement this code with anchor tags. It works well with the standard layout, but as soon as I implement an anchor tag the accordion will not work!!!!
Thanks in advance!
@Ryan: do you have any advice for me? I’m using the accordion inside a wrapper div, and I want to put a new withing th wrapper, but always below the accordion content, no matter which pane is open. Can you suggest a method for this?
can this be set to auto play some how
[...] Stupid Simple jQuery Accordion MenuMar 8, 2009 … While the accordion system I used on these sites worked well, I decided to write my own using jQuery with the goal of making it as simple as … [...]
Great script!!! I need content to open up from right to left not top to bottom! Anyone have a fix for this plse. Thanks – TD
Duuuude, so simple, thank you so much for this, seriously.
Hello Ryan
i ma modifying this web and intend to use the Accordion menu shown stupid simple Accordion .
However i need to open in side the Buttons , 3 new Accordion Buttons , so i ma looking fo a nested Accordeon solution , can you please advise how to proceed .
I reeally appreciate your help Regards
Muradams
thanks for your post
Hello
Great example thank you!
Can you please advice how could the selected menu stays open when a sub item is selected…sorry if has been already said.
thank you
Here is my version of your menu a website I recently designed. Thanks a ton for this Ryan. http://andycartwright.biz/cone-lamp-shade/
I found a better way to keep a panel open, use this:
$(“div.open”).not(“.open”).hide();
Then on the content you want to stay visible on page load you add the class open.
This prevents the area from springing open on page load.
Thanks for this, I’ve been beating my head up trying to get this working with wordpress, and this simple is AMAZING.
I’m still hanging out for the horizontal sliding version, would love to see that !!
I need your help pleaseeee! The tutorial is excelente but i have a problem I put it as a galery content and when i click on them my foot stays and doesnt slide down with the accordeon efect, somebody can help me with this.
Thanks
Very easy to follow. I was using the examples from the jQuery UI site and was not having a lot of success. This example was up and running on my site in a matter of minutes.
Thanks!
Ryan,
Like so many other people, THANKS SO MUCH for this, it has really helped me as well.
I’m sure you’ve addressed this earlier, but I’ve searched forever and couldn’t find a solution… plus, it seems like it was part of the original download.
On load, the first address is displayed [good]. But if you click on a slide while it is active, it closes [bad].
My client wants me to disable the functionality to the active slide.
You can see my example here:
http://virtual.jeffstateonline.com/
I’ve also combined it with the jquery cycle plugin so that the image changes when the accordion is clicked.
Thanks again.
Hello,
Is it possible to have an external menu page that opens each part separately?
Thank you in advance,
[...] MORE INFO | DEMO – by Ryan Stemtoski (Free Plugin/Tutorial) [...]
Cool script. How would you make the first area expanded as a default. So when you land on the page the first one will always be open showing something.
Cheers!
Wow, this one was stupid simple!
First of all, thank you so much for posting this. It is incredibly helpful. I’m not sure if you’re still responding to this post because it’s 3 1/2 years old now, but I have a question, if you are.
see the page where I used this here: http://recruitingprocessanalytics.redbranchmedia.com/resources/faqs/
If I click on the accordion button, it opens the content as expected. If I click on the same button again, it closes the content as expected. (I added the addt’l code you gave) However, if I click on that same button a THIRD time, nothing happens. It simply won’t open again until I click on one of the other accordion buttons. Any idea why or how I fix that? Thank you for any help you can give.
Just ignore my previous comment. This is now working as it should. I added a line to the code that I found in the comments. Here is my full code: ‘$(document).ready(function() {
//ACCORDION BUTTON ACTION
$(‘div.accordionButton’).click(function() {
if($(this).is(“.accordionButtonSelected”)){
$(‘div.accordionContent’).slideUp(‘normal’);
$(this).removeClass().addClass(‘accordionButton’);
}
else {
$(‘div.accordionContent’).slideUp(‘normal’);
$(this).next().slideDown(‘normal’);
$(‘div.accordionButtonSelected’).removeClass().addClass(‘accordionButton’);
$(this).removeClass().addClass(‘accordionButtonSelected’);
}
});
//HIDE THE DIVS ON PAGE LOAD
$(“div.accordionContent”).hide();
});
‘
Hello, great little script here.
I am having the flickering problem in Chrome (on a mac. FF and Safari are smooth as butter).
The only way to fix it seems to be to give the div that the accordion is in (#wrapper in the demo) a height. Which I obviously can’t do as the accordion stretches beyond this height when a long content box is opened. Or alternatively there is a long gap under the accordion when a user first visits.
Anyone happen to have any ideas?
hello:
We tried to create the function “mouseover” but when the “div” contains elements like “h1″ interprets “h1″ is again required class for the event and run the action again. Maybe someone can help us.
Here’s the html code:
Button 1 TittleButton 1 Subtittle
Content 1Long Example
Button 2 Content
Content 2Medium Example
Button 3 Content
Content 1Short Example
Button 4 Content
Content 4Extra Long Example
Here’s the js code:
$(document).ready(function() {
$(‘.accordionButton’).mouseover(function() {
if($(this).next().is(‘:hidden’) == true) {
$(this).addClass(‘over’);
$(this).next().slideDown(‘normal’);
}
});
$(‘.accordionButton’).mouseout(function() {
if($(this).next().is(‘:visible’) == true) {
$(this).next().slideUp(‘normal’);
$(this).removeClass(‘over’);
}
});
$(‘.accordionContent’).hide();
});
Thank you very much.
Thanks for sharing this examples, is there any way to do that to be autplay
[...] 3. Stupid Simple jQuery Accordion Menu [...]
[...] Accordion Menu [...]
[...] http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/ [...]
[...] used this tutorial http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/ to built it and all works fine except, every time I select a new item the whole menu jumps up [...]
How to use stupid simple jquery accordion in wordpress.
Your accordion works fine. However, I need to add a plus image (collapsed) and minus image (expanded) to the accordion.
Thank you for your help
I wonder if you can help?
I am successfully using your script on a shopping cart website I am developing for a mate of mine (http://petesbiltong.com). However, I have a small problem, which I can’t solve with my limited knowledge of javascript.
I want to link back to the accordion, and automatically open the shopping cart which is the 3rd accordion panel.
I’ve tried all sorts of javascript workarounds to no avail (var link = document.getElementById['accordionButton'][2];
link.click(); … being the most recent)
I would greatly appreciate any feedback you can offer (‘cos I’m tearing my hair out at this stage!)