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

Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio

Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.


Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.


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.


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.


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!
Vu 32'' TV Vu 24'' TV
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