- Posted On 20 January 2015
- By
- In Programming
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!!
- Tags :
- jQueryJavascript
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.
Best CSS Gradient background generator tools online
Here are some best CSS gradient background code generator online tools using which you can create a cross browser css code for gradient backgrounds.