Can I specify multiple data targets for Twitter Bootstrap collapse?

You can simply add all the ids separated by commas in data-target:

<button type="button" data-toggle="collapse" data-target="#demo,#demo1,#demo2">
  Hide them all
</button>

Use the same class for both div's and set your data-target according this. Give your div's also the same parent (can also be a class) and set data-parent according this.

<button type="button" data-parent="#wrap" data-toggle="collapse" data-target=".demo">
    simple collapsible
</button>
<div id="wrap">
    <div class="demo collapse">
        test1
    </div>
    <div class="demo collapse">
        test1
    </div>
</div>

From the Bootstrap 3 documentation:

The data-target attribute accepts a CSS selector to apply the collapse to.

Therefore you can use a comma-separated list of id's, class selector etc. for the data-target attribute value:

<button class="btn btn-primary" type="button"
    data-toggle="collapse" data-target="#target1,#target2,#target3"
    aria-expanded="false" aria-controls="target1,target2,target3">

You can see an extensive list of valid CSS selectors on the w3schools website.


If you want to hide one element and show another (like the bootstrap accordion but without a panel) you can add multiple targets but also add the class 'in' to have one element expanded on load!

<div class="collapse text-center clearfix in" id="section1">
   <h1>This is section 1</h1>
   <p>Are you excited about toggling?</p>
</div>
<div class="collapse text-center clearfix alert-warning" id="section2">
   <h1>Boo!!</h1>
   <p>This is a new sentence</p>
</div>
<button class="btn btn-block btn-primary" data-toggle="collapse" data-target="#section1,#section2">Toggle</button>

Live demo here


There is a solution in Bootstrap 4:

Bootstrap 4 documentation: Multiple targets

The least you need is:

  • data-toggle="collapse" data-target=".multi-collapse" for the toggle button
  • class="collapse multi-collapse" for each element you need to toggle

(While multiple ids in data-target indeed don't work).