Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

How to create simple accordion using jQuery, HTML & CSS


Share with WhatsApp


In this simple post I am going to share and explaing the simple jQuery code to create a simple accordion.

Though, accordion is available in jQuery UI and Bootstrap but still if you are not using jQuery UI and Bootstrap and want to create a simple accordion then you can achieve the same by following code.

First,I have created a HTML code like below.

<div class="accordion">
   <a href="#" >Anchor 1</a>
</div>
<div class="accordion-body">
   This is the div to show 1
   <p>Lorem Ipsum.....</p>
</div>
<div class="accordion">
   <a  href="#" >Anchor 2</a>
</div>
<div class="accordion-body">
   This is the div to show 2
   <p>Lorem Ipsum...</p>
</div>
<div class="accordion">
   <a href="#" >Anchor 3</a>
</div>
<div class="accordion-body">
   This is the div to show 3
   <p>Lorem Ipsum....</p>
</div>
<div class="accordion">
   <a href="#" >Anchor 4</a>
</div>
<div class="accordion-body">
   This is the div to show 4
   <p>Lorem Ipsum....</p>
</div>

Then I also added some basic CSS code to make our accordion look better.

.accordion {
background-color:#ddd;
background-image:linear-gradient(#ddd,#ededed);
cursor:pointer;
padding:15px;
}

.accordion a {
text-decoration:none;
color:#000;
}

.accordion-body {
display:none;
border:1px solid #ddd;
border-top:none;
padding:10px;
}

Now its time to convert this basic div structure in working accordion. For that, I have added small jQuery code like below.

$(".accordion").on("click",function()
{ 
   var accordianBody = $(this).next();                     
   $(".accordion-body").not(accordianBody).slideUp("slow"); 
   $(accordianBody).slideToggle();

});

That's it. Our "Accordion" is ready which you can see live in below jsFiddle.

Now lets understand the jQuery code. HTML and CSS is very basic which you can understand easily.

There is a div having class "accordion" and its body in div which has class named "accordion-body". In jQuery we have handled a click event for "accordion" div. 

In click event, we first created an object of its related div i.e. "accordion-body" by using below line.

var accordianBody = $(this).next();

Then, I have set all accordion body divs in hidden state by using below line of code. In this I have used .not() function which restricts the behaviour of hiding and showing the accordion body if user clicks on accordion div more than once.

$(".accordion-body").not(accordianBody).slideUp("slow"); 

Now using below line of code I have toggled the state of accordion body.

$(accordianBody).slideToggle(); 

Hope you have liked this simple post. Thanks. Keep reading, keep sharing!!



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


Speed up coding in Visual Studio with code snippets & samples at your fingertips

Know how you can speed up coding in Visual Studio with Bing Developer Assistant by having millions of code snippets and sample projects at fingertips.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x