An accordion effect is a nice way to efficiently use the limited space available for menus.

In this simple example the staring HTML is a nested <ul> list.

The HTML is:

       <li class="top">item 1
       	<li>item 1 Sub 1</li>
        <li>item 1 Sub 2</li>
        <li>item 1 Sub 3</li>

       <li class="top">item 2
       	<li>item 2 Sub 1</li>
        <li>item 2 Sub 2</li>
        <li>item 2 Sub 3</li>
       </ul> </li>

       <li class="top">item 3
       	<li>item 3 Sub 1</li>
        <li>item 3 Sub 2</li>
        <li>item 3 Sub 3</li>
        <li>item 3 Sub 4</li>
        <li>item 3 Sub 5</li>

       </ul> </li>
       <li class="top">item 4
       	<li>item 4 Sub 1</li>
        <li>item 4 Sub 2</li>

The CSS for this is as follows:

nav ul{
nav ul li{
    cursor: pointer;
    font-weight: bold;
    font-size : 14px;
    line-height: 30px;
    background: #e3e3e3;
    border-bottom: 1px solid #c5c5c5;
    border-top: 1px solid white;
nav ul li a{
nav ul li:first-child{
	border-top: none;
nav ul li:last-child{
	border-bottom: none;
nav ul li ul{
nav ul li ul li{

The key line here is the display:none set for the child <ul> elements.

The jQuery is incredibly compact.

	   var menuItem = $("nav ul");
	   menuItem.on("click", function(){  
		  var clickedItem = $(this).index();
			 if(clickedItem != i){

View the demo of this.


Still here? Then you must want the explanation.

First up we create a jQuery selector for the top level menu items – those marked as class=”top” in the HTML.

An ‘on’ event is attached to this selector listening out for the ubiquitous click.

The goal is to have any menus that are already rolled down, roll up, and have the clicked menu roll down.

The roll down is easy to do and will be done last. To roll up any menus currently rolled down we’ll loop around the list and use the slideUp() method. The loop is performed with a jQuery each. To prevent the clicked item from rolling down before the loop is executed we find the index() of the clicked element.

The index() of the clicked element is an integer representing the order of the element amongst its siblings – that is all the other ‘top’ level menu items. With this valued stored as the variable clickedItem, we use an if statement inside the loop to prevent the slideUp() on the clicked list item.

Finally the children of the clicked element, that is all the child <li> elements are feed to the slideToggle() method to slide Up or Down the nested list dependent on its starting state.

Read more about animation methods.

Leave a Comment