Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap 2 on collapse event

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.

like image 304
itsme Avatar asked May 17 '12 18:05

itsme


2 Answers

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'); });
like image 146
patspam Avatar answered Sep 27 '22 22:09

patspam


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

like image 36
nic Avatar answered Sep 27 '22 20:09

nic