Bootstrap Accordion Panel/Collapse

tom1bomb picture tom1bomb · Jan 27, 2014 · Viewed 30.3k times · Source

My bootstrap accordian is acting temperamental! I have created three panels displayed in a column grid, but the 2nd and 3rd collapse menu closes the wrong menu (e.g. clicking/opening menu2 then clicking/opening menu3 will close menu2 before opening menu3).

Any idea why?

Code here:

        <div class="row">
            <div class="col-sm-6 col-md-4">
                <div class="panel-group">
                    <div class="panel panel-default">
                        <a class="panel-default" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                            My Story
                            </h4>
                        </div>
                        </a>
                        <div id="collapseOne" class="panel-collapse collapse">
                            <div class="panel-body">
                            Content
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-md-4">
                <div class="panel-group" id="accordion">
                    <div class="panel panel-default">
                        <a class="panel-default" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                            My Skills
                            </h4>
                        </div>
                        </a>                            
                        <div id="collapseTwo" class="panel-collapse collapse in">
                            <div class="panel-body">
                            Content
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6 col-md-4">
                <div class="panel-group" id="accordion">
                    <div class="panel panel-default">
                        <a class="panel-default" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                        <div class="panel-heading">
                            <h4 class="panel-title">
                            My Ambition
                            </h4>
                        </div>
                        </a>                            
                        <div id="collapseThree" class="panel-collapse collapse">
                            <div class="panel-body">
                            content
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

Answer

Zeeba picture Zeeba · Jan 27, 2014

You need to number the accordion i.e. accordion1 accordion2 accordion3

<div class="row">
    <div class="col-sm-6 col-md-4">
        <div class="panel-group">
            <div class="panel panel-default">
                <a class="panel-default" data-toggle="collapse" data-parent="#accordion1" href="#collapseOne">
                <div class="panel-heading">
                    <h4 class="panel-title">
                    My Story
                    </h4>
                </div>
                </a>
                <div id="collapseOne" class="panel-collapse collapse">
                    <div class="panel-body">
                    Content
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-md-4">
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
                <a class="panel-default" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
                <div class="panel-heading">
                    <h4 class="panel-title">
                    My Skills
                    </h4>
                </div>
                </a>                            
                <div id="collapseTwo" class="panel-collapse collapse in">
                    <div class="panel-body">
                    Content
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-sm-6 col-md-4">
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
                <a class="panel-default" data-toggle="collapse" data-parent="#accordion3" href="#collapseThree">
                <div class="panel-heading">
                    <h4 class="panel-title">
                    My Ambition
                    </h4>
                </div>
                </a>                            
                <div id="collapseThree" class="panel-collapse collapse">
                    <div class="panel-body">
                    content
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>