Twitter download Make the first accordion panel open by default

I use twitter bootstrap to make accordions on the page. I want the first panel to open on every page. This is the code I wrote:

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

This, however, adds the in class to all elements. What needs to be fixed?

+4
source share
2 answers

You add the class to the entire accordion_list element collection, but you need to add it to the current iterative element.

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

If you do nothing in the loop, you can also make it shorter:

 accordion_list.first().addClass("in"); 
+6
source

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

 $("#accordion").find('.panel-collapse:first').addClass("in"); 
+2
source

All Articles