0

I have done research and to my knowledge data-target is what i need to close down the div, if the other open. In other words I need to open one div at a time. But i think i am unable to do that or cannot make sense on how to open one accordion at a time. Here is my code

 <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> 

1 Answer 1

2

You can use the Bootstrap Collapse API to watch for show.bs.collapse event triggers firing, then closing other collapse elements using the collapse('hide') method. The code for that would be as follows:

$(document).ready(function(){ var $panels = $('.panel-collapse'); $panels.on('show.bs.collapse', function(){ $panels.not(this).collapse('hide'); }); }); 

Here is an updated codepen also with some updated HTML.

Note, I'd recommend to never have a row directly within another row, there simply isn't any need for it and can create issues with the grid. Also, you may not need as many container classes as you have, you can simply have the wrapping containers to effectively contain the contents.

Hopefully this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Sometimes I get lost in Bootstrap documentation.
I feel you, it can definitely get overwhelming, but once you can navigate through it you find some very exciting things you can do with their API. Hopefully the answer helped.
What does the $panel.on do?
$panels a variable that is simply storing the .panel-collapse items so that they don't get queried/retrieved each time the event fires. jQuery on() is a method for attaching events to jQuery elements. on() is needed in this case because we are looking for a custom Bootstrap event rather than something standard like click() or focus(). In this case it will listen for any show.bs.collapse events (when panel open is triggered) then execute the anonymous function executing hide on other collapse elements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.