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>
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 .
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 .
Bootstrap uses the aria-expanded attribute to show true or false if the region is collapsed or not.
var isExpanded = $(collapsableRegion).attr("aria-expanded");
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With