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?
If you are using bootstrap accordion and want that one of the accordion should be opened for the first time so add class="in" .
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.
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});
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.
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");
If you want to show the first panel in the accordion:
$("#accordion").find('.panel-collapse:first').addClass("in");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With