Simple JQuery Accordion menu

Post Date: May 12 2009
Total Visit: 31081

Simple JQuery Accordion menu is a best handy tool to show and hide information as needed. It also feel like a flash menu. I have used them on sites like Content Management System, Marketplace, E-commerce, and some of the Lowest/Highest Unique Bids Auction website. This accordion menu is worked perfectly while I used on these website. So, Now I decided to show how to create jQuery accordion menu which will help to your website too...

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. This can be done by including the following code in the head of your page:

<script type="text/javascript" language="javascript" src="jquery.js"></script> Click here to download.

Step 2: Create the JavaScript:

Next step you need to create a blank JavaScript file. For the example, this file is called jquery-accordion.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 after the jQuery library. An example of the JavaScript code to include in this document is shown below:

function initMenu()
     $('#menu ul').hide();
     $('#menu ul:first').show();
     $('#menu li a').click(function()
          var checkElement = $(this).next();
          if(('ul')) && (':visible')))
               return false;
          if(('ul')) && (!':visible')))
               $('#menu ul:visible').slideUp('normal');
               return false;
$(document).ready(function() {initMenu();});

Step 3: Create the CSS:

Now we need to create a CSS file which will include  in the head of your page. For the example, this file is called style-accordion.css.This file will contain the CSS code for accordion menu:

#menu{font-family: Helvetica, Arial, sans-serif;  font-size: 0.9em;}
ul#menu, ul#menu ul {      list-style-type:none;  margin: 0;  padding: 0;  width: 15em;}
ul#menu a {  display: block;  text-decoration: none;    }
ul#menu li {  margin-top: 1px;}
ul#menu li a {  background: #006600;  color: #fff;      padding: 0.5em;}
ul#menu li a:hover {  background: #006600;}
ul#menu li ul li a {  background:#F7F7F7;  color: #000;  padding-top: 3px;  padding-bottom: 3px;  line-height: 14px;  background-image: url(arrow.gif);  background-repeat: no-repeat;  padding-left: 15px;   border-bottom:0px;}
ul#menu li ul li a:hover {  background: #BFBFBF;  border-left: 5px #006600 solid;  padding-left: 10px;}

Step 4: Create the HTML:

Now we need to create the HTML page to manipulate it. For the example, this page is called accordion.html. we will keep the page very, very simple. While page load there is dispaly first UL element menu by default. After that when you click on the any menu then jQuery functoin will check clickable element is visible , if yes then it will return false otherwise it will display clickable list menu and hide others.The full HTML document also displaying the includes is shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Simple JQuery Accordion menu</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery-1.2.1.min.js" type="text/javascript"></script>
<script src="menu.js" type="text/javascript"></script>
<!--[if lt IE 8]>
<style type="text/css">
li a {display:inline-block;}
li a {display:block;}
<ul id="menu">
<a href="#">Programming Languages</a>
<li><a href="">PHP</a></li>
<li><a href="">Ruby</a></li>
<li><a href="">Python</a></li>
<li><a href="">PERL</a></li>
<li><a href="">Java</a></li>
<li><a href="">C#</a></li>
<a href="#">Content Management System</a>
<li><a href="">Joomla</a></li>
<li><a href="">Drupal</a></li>
<li><a href="">Pligg</a></li>
<li><a href="">Modxcms</a></li>

<li><a href="">SKS's Home (no submenu)</a></li>
<a href="#">Web Development Services</a>
<li><a href="">Content Management System</a></li>
<li><a href="">E-commerce Website</a></li>
<li><a href="">Small Business Website</a></li>
<li><a href="">Lowest Unique Bids Auction</a></li>
<li><a href="">Highest Unique Bids Auction</a></li>
<li><a href="">Penny/Live Bids Auction Script</a></li>
<a href="#">Search Engines</a>
<li><a href="">Yahoo!</a></li>
<li><a href="">Google</a></li>
<li><a href=""></a></li>
<li><a href="">Live Search</a></li>


Tags: ajax, jQuery, According Menu, jQuery Accordion Menu Bookmark and Share

To do articles more effectively, we need your comments or suggestions on how to improve the articles and make it more useful, or what other articles you would like in the future. Please CLICK HERE and leave your feedback. Thanks!!!