Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding open/closed icon to Twitter Bootstrap collapsibles (accordions)

I've set up this simple version of the Bootstrap accordion:

Simple accordion: http://jsfiddle.net/darrenc/cngPS/

Currently the icon only points down, but does anyone know what JS would be needed to be implemented so as to change the class of the icon to:

<i class="icon-chevron-up"></i> 

...so that it points up when expanded and toggles back to down again when collapsed, and so fourth?

like image 844
Darren Avatar asked Dec 08 '12 15:12

Darren


People also ask

How do I add an icon to my accordion?

You can add the icon custom css class to the Accordion header using 'iconCss' property and also add css styles to the defined class. The accordion icon element is rendered before the header text in the DOM element.

How do I keep my Bootstrap accordion open?

Just add data-toggle="collapse" and a data-target to element, to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.


1 Answers

Here it's the answer for those who are looking for a solution in Bootstrap 3(like myself).

The HTML part:

<div class="btn-group">   <button type="button" class="btn btn-danger">Action</button>   <button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#demo">     <span class="glyphicon glyphicon-minus"></span>   </button> </div> <div id="demo" class="collapse in">Some dummy text in here.</div> 

The js part:

$('.collapse').on('shown.bs.collapse', function(){ $(this).parent().find(".glyphicon-plus").removeClass("glyphicon-plus").addClass("glyphicon-minus"); }).on('hidden.bs.collapse', function(){ $(this).parent().find(".glyphicon-minus").removeClass("glyphicon-minus").addClass("glyphicon-plus"); }); 

Example accordion:

Bootply accordion example

like image 125
Andrei Veve Avatar answered Sep 18 '22 13:09

Andrei Veve