Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate a click to open Bootstrap collapse element

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 ?

like image 383
AlexB Avatar asked Jun 05 '14 07:06

AlexB


People also ask

How do I keep my Bootstrap collapse Open?

Just add data-toggle="collapse" and a data-target to element, to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in .

How do I make accordion open by default in Bootstrap?

If you'd like it to default open, add the additional class show . To add accordion-like group management to a collapsible area, add the data attribute data-parent="#selector" .

How do you implement Bootstrap collapse?

To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element. Then add the data-target="#id" attribute to connect the button with the collapsible content (<div id="demo">).

How do you expand a div collapse?

To make an animated collapsible, add max-height: 0 , overflow: hidden and a transition for the max-height property, to the panel class.


1 Answers

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

like image 95
Rory McCrossan Avatar answered Oct 01 '22 07:10

Rory McCrossan