Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a bootstrap collapse is opening or closing?

If I have a bootstrap collapse how can I determine from a click event wether the collapse is opening or closing?

Here is my click event or maybe there is a better way then to use a click event?

$(document).on("click", "a.register-student-link", function() {     // do some stuff to check if opening or closing } 

<div>    <a [email protected] class="register-student-link" data-toggle="collapse" href=@spaceIdWith aria-expanded="false" aria-controls="collapseExample">                                                      Register Student                                                  </a>  </div>
like image 307
user1186050 Avatar asked Oct 13 '15 00:10

user1186050


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 .

What is collapsing in Bootstrap?

The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 .


2 Answers

Bootstrap uses the aria-expanded attribute to show true or false if the region is collapsed or not.

var isExpanded = $(collapsableRegion).attr("aria-expanded"); 
like image 109
user1186050 Avatar answered Oct 05 '22 11:10

user1186050


Try:

$('#collapseDiv').on('shown.bs.collapse', function () {     console.log("Opened")  });    $('#collapseDiv').on('hidden.bs.collapse', function () {     console.log("Closed")  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>  <div>    <a [email protected] class="register-student-link" data-toggle="collapse" href="#collapseDiv" aria-expanded="false" aria-controls="collapseExample">Register Student</a>  </div>    <div class="collapse" id="collapseDiv">This is the collapsible content!</div>

(based on https://stackoverflow.com/a/18147817/2033574 (Kevin mentioned) and http://www.bootply.com/73101)

like image 31
Josiah Krutz Avatar answered Oct 05 '22 13:10

Josiah Krutz