Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap Make first panel of accordion open by default

I am using twitter bootstrap to make accordions on the page. I want to make the first panel open on each page. This is the code that I wrote:

    var accordion_list = $(".prime-content .accordion-body");
    accordion_list.each(function(index, element){
      accordion_list.addClass(index == 0 ? "in" : "");
    });

This however, is adding "in" class to all the elements. What needs to be fixed?

like image 739
Aakash Goel Avatar asked Aug 01 '12 07:08

Aakash Goel


People also ask

How do I make my first accordion open by default?

If you are using bootstrap accordion and want that one of the accordion should be opened for the first time so add class="in" .

How do I keep my bootstrap accordion open by default?

If you'd like it to default open, add the additional class show . To add accordion-like group management to a collapsible area, add the data attribute data-parent="#selector" . Refer to the demo to see this in action.

How do you make an accordion collapse by default?

To create an accordion that is collapsed by default, we need to set the 'active' property of the jQuery Accordion as false. Syntax: $("#demoAccordion"). accordion({ collapsible: true, active: false});

How do I collapse open by default?

Make expand/collapse controls accessible If you've set the collapsible element to be open by default using the in class, set aria-expanded="true" on the control instead. The plugin will automatically toggle this attribute based on whether or not the collapsible element has been opened or closed.


2 Answers

You are adding the class to the whole accordion_list collection of elements but you need to add it to the currently iterated element instead.

$(".prime-content .accordion-body").each(function(index, element){
  $(element).addClass(index == 0 ? "in" : "");
});

If you don't do anything else in the loop you can also make it shorter:

accordion_list.first().addClass("in");
like image 68
kapex Avatar answered Oct 03 '22 07:10

kapex


If you want to show the first panel in the accordion:

$("#accordion").find('.panel-collapse:first').addClass("in");
like image 35
Diogo Gomes Avatar answered Oct 03 '22 06:10

Diogo Gomes