Twitter Bootstrap 2 on collapse event
Asked Answered
N

4

11

is there any way to catch when the collapse menu appears (or when collapse button is clicked by user) ?

i'm using standard bootstrap twitter framework and classes.

Noncooperation answered 17/5, 2012 at 18:42 Comment(1)
You want to catch the collapse event on your whole document or just a section/specific menu?Marroquin
C
2

TWITTER BOOTSTRAP 2

It looks like the plugin is adding the class "in" to the active collapse group. So maybe you could do something like.

if( $('#collapseOne').hasClass('in') ){
..do something
}

EDIT:

Okay, I don't have anyway of testing right now, so these are kind of just guesses. I am sure you could easily determine when a collapse link was clicked by doing this.

$('.accordion-toggle').click(function(){
   console.log('clicked');
});

However if you wanted to know exactly which one was clicked you would probably have to index them.

Carniola answered 17/5, 2012 at 18:53 Comment(1)
BS 3.0 has four events show.bs.collapse, shown.bs.collapse, hide.bs.collapse and hidden.bs.collapseTessin
R
34

In bootstrap 3.x, the four events you're looking for are:

show.bs.collapse
shown.bs.collapse
hide.bs.collapse
hidden.bs.collapse

They fire on the data-target DOM element, e.g. for the following mark-up:

<div role="navigation" class="navbar navbar-inverse">
  <div class="container">
    <div class="navbar-header">
      <button type="button" data-toggle="collapse" data-target=".navbar-collapse" class="navbar-toggle">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="#" class="navbar-brand">Title</a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li id="item1"><a href="#item1">Menu Item 1</a></li>
      </ul>
    </div>
  </div>
</div>

You'd listen (in jQuery) via:

$('.navbar-collapse').on('shown.bs.collapse', function() { alert('shown'); });
$('.navbar-collapse').on('hidden.bs.collapse', function() { alert('hidden'); });
Reason answered 25/1, 2014 at 17:25 Comment(1)
The questioner is looking for help with Bootstrap 2, not 3Venatic
R
9

Use the "shown" event handler:

$("#accordion").on("shown",function(event){
        collapse_element = event.target;
    });

Refer to: http://twitter.github.com/bootstrap/javascript.html#collapse to see the other events supported

Rockbound answered 3/1, 2013 at 23:50 Comment(1)
Unfortunately though I successfully got an accordion with this event working in this fiddle it doesn't seem to want to work in Bootstrap 3.0, and the link provided is now broken. Continuing on the search...Ethanol
B
3

If you want to use easing animation with the changing of the icon you need to do this on show/hide.

In this example, I'm using bootstrap 2.3.2, jQuery 1.8.3, and Font Awesome 3.2.1

HTML:

<div id="accordion" class="accordion">
  <div class="accordion-group">
    <div class="accordion-heading">
      <a data-item-id="{id}" href="#something" data-parent="#accordion"
         data-toggle="collapse" class="accordion-toggle">
       <i class="icon icon-expand-alt"></i> {title}</a>
     </div>
     <div data-item-id="{id}" class="accordion-body collapse" id="something">
       <div class="accordion-inner">
         {Content}
       </div>
     </div>
  </div>
 ... {additional panels}
</div>

js:

$('#accordion').on('show hide', function (e) {
  var oTarget = $(e.target);
  var oIcon = $('i', $('a[data-item-id="' + oTarget.attr('data-item-id') + '"]'));
  oIcon.toggleClass('icon-expand-alt icon-collapse-alt', 200);
 });
Bobcat answered 18/11, 2013 at 23:57 Comment(0)
C
2

TWITTER BOOTSTRAP 2

It looks like the plugin is adding the class "in" to the active collapse group. So maybe you could do something like.

if( $('#collapseOne').hasClass('in') ){
..do something
}

EDIT:

Okay, I don't have anyway of testing right now, so these are kind of just guesses. I am sure you could easily determine when a collapse link was clicked by doing this.

$('.accordion-toggle').click(function(){
   console.log('clicked');
});

However if you wanted to know exactly which one was clicked you would probably have to index them.

Carniola answered 17/5, 2012 at 18:53 Comment(1)
BS 3.0 has four events show.bs.collapse, shown.bs.collapse, hide.bs.collapse and hidden.bs.collapseTessin

© 2022 - 2024 — McMap. All rights reserved.