Simulate a click to open Bootstrap collapse element

AlexB picture AlexB · Jun 5, 2014 · Viewed 22.3k times · Source

I'm trying to simulate a clik on a Bootstrap collapse header, but without success.

What I really want to do is, when user clicks on an image near to the header of the accordion, it opens exactly like if the user had clicked on the <a> of the header.

Here is a FIDDLE for a better understanding

My HTML markup looks like this :

<div class="accordion" id="accordionFilter">
    <div class="accordion-group">
        <div class="accordion-heading">
          <img src="http://placehold.it/40x40" onclick="SimulateClick();">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordionFilter" href="#collapseOne">
                Filter the results
            </a>
        </div>
        <div id="collapseOne" class="accordion-body collapse">
            <div class="accordion-inner">
                <div class="row">
                    <br>
                    <div class="col-lg-3 col-md-3 col-sm-3">
                        1st column content
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-3">
                         2nd column content
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

But I have no idea how to write "on click on the image, open the accordion" in the SimulateClick() JS method.

I tried with something like

$("#collapseOne").click();

or

$("#collapseOne").addClass("in");

but it doesn't do anything.


Any idea ?

Answer

Rory McCrossan picture Rory McCrossan · Jun 5, 2014

You don't need to simulate a click as bootstrap exposes methods you can use to control the plugins programmatically. In this case there is a 'toggle':

$('.accordion-heading img').click(function() {
  $('#collapseOne').collapse('toggle');
});
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<div class="accordion" id="accordionFilter">
  <div class="accordion-group">
    <div class="accordion-heading">
      <img src="http://placehold.it/40x40">
      <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordionFilter" href="#collapseOne">
        Filter the results
      </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse" style="height: 0px;">
      <div class="accordion-inner">
        <div class="row">
          <br>
          <div class="col-lg-3 col-md-3 col-sm-3">
            1st column content
          </div>
          <div class="col-lg-3 col-md-3 col-sm-3">
            2nd column content
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

More info on the available methods in the API: http://getbootstrap.com/javascript/#collapse-usage