<ul id="accordion">
<li><div>Sports</div>
<ul>
<li><a href="#">Golf</a></li>
<li><a href="#">Cricket</a></li>
<li><a href="#">Football</a></li>
</ul>
</li>
<li><div>Technology</div>
<ul>
<li><a href="#">iPhone</a></li>
<li><a href="#">Facebook</a></li>
<li><a href="#">Twitter</a></li>
</ul>
</li>
<li><div>Latest</div>
<ul>
<li><a href="#">Obama</a></li>
<li><a href="#">Iran Election</a></li>
<li><a href="#">Health Care</a></li>
</ul>
</li>
</ul>
Code language: HTML, XML (xml)
If you check the HTML page now it should look something like: #accordion {
list-style: none;
padding: 0 0 0 0;
width: 170px;
}
#accordion div {
display: block;
background-color: #FF9927;
font-weight: bold;
margin: 1px;
cursor: pointer;
padding: 5 5 5 7px;
list-style: circle;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
#accordion ul {
list-style: none;
padding: 0 0 0 0;
}
#accordion ul{
display: none;
}
#accordion ul li {
font-weight: normal;
cursor: auto;
background-color: #fff;
padding: 0 0 0 7px;
}
#accordion a {
text-decoration: none;
}
#accordion a:hover {
text-decoration: underline;
}
Code language: CSS (css)
Note that in above CSS code, we have applied Round Corner CSS to our menu to improve the look. Although this technique works with all the modern web browser, but it will not work with Internet Explorer. If we want to change the look n feel for internet explorer, we may want to include IE-Specific stylesheet to our HTML page. Once we apply the stylesheet, our menu will look like: $("#accordion > li > div").click(function(){
if(false == $(this).next().is(':visible')) {
$('#accordion ul').slideUp(300);
}
$(this).next().slideToggle(300);
});
$('#accordion ul:eq(0)').show();
Code language: JavaScript (javascript)
If you notice the above code, we have made the first menu item in accordion menu visible. $('#accordion ul:eq(0)').show();
Code language: JavaScript (javascript)
To make a menu item visible by default, just add its index value to the above code. For example to display 1st menu item we added eq(0)
in above code. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
NIce post , i will try to work it out
This is cool! Thanks for sharing
Nice menu, but it would be better if the html code was valid (whether it is html or xhtml).
The "level-two" list blocks (ul) should be nested in the inherent item, like explained in the third example there : http://reference.sitepoint.com/html/ul
@Cilia - I have modified the source and made it valid HTML. Have a look. Now the sub
<UL>
is part of main accordion<LI>
. This should not break the HTML validation now.your site is very helpful
him
very nice post ...
tested in IE6,7,8- it doesn't work..
why do you ven us IE7 some people went in prison because they used it
Thanks, nice and in ul li great
If you use valid HTML (as cilia pointed out above), then this worked for me:
$("#accordion > li").click(function(){
if(false == $(this).children().is(':visible')) {
$('#accordion > li > ul').slideUp(300);
}
$(this).children().slideToggle(300);
});
$('#accordion > li > ul:eq(0)').show();
You may want to modify the css as the rounded corners don't look good as the ul is now inside the li.
... and if you have a menu where some items do not have submenus, then add the 'ul' filter:
$(this).children('ul').slideToggle(300);
otherwise it will roll up itself.
thats awsome for a biginner ....
Hi,
This is not work on IE 6/7. Please anybody can tell me how it can be possible??
Thanks,
Pravin.
nice tutorial, I have also wrtten a simple on on my blog here:
http://ranacseruet.blogspot.com/2009/12/simple-accordion-using-jquery.html