35

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 id=@space.EventId class="register-student-link" data-toggle="collapse" href=@spaceIdWith aria-expanded="false" aria-controls="collapseExample">
                                                    Register Student
                                                </a>
</div>
user1186050
  • 10,710
  • 18
  • 92
  • 235

5 Answers5

37

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

var isExpanded = $(collapsableRegion).attr("aria-expanded");
user1186050
  • 10,710
  • 18
  • 92
  • 235
  • 3
    For anyone struggling with getting this to work; make sure you initialise the `aria-expanded` attribute to `true` or `false` in the HTML! – slugmandrew Apr 10 '19 at 15:47
  • This might be true and probably helped you, seeing this answer is made by the OP. However, if I read through the question itself (which I had too), this does not answer it at all. The answer below (with shown.bs.collapse) works like a charm and answer the true question of "how to determine the collapse event". I think that should be marked as the correct answer. – Kalko Jun 04 '19 at 14:30
  • Thanks. This helped! – Adil Aslam Sachwani Jan 16 '21 at 14:13
33

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 id=@space.EventId 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)

Community
  • 1
  • 1
Josiah Krutz
  • 935
  • 6
  • 15
24

I needed a way to determine if the element was collapsed BEFORE actually collapsing. Whereas the event listeners only trigger afterwards.

//Will return true if uncollapsed
$('#collapseDiv').hasClass('in');

//Will return true if in the process of collapsing
$('#collapseDiv').hasClass('collapsing');

//Will return true if collapsed
$('#collapseDiv').hasClass('collapse');
adam3039
  • 1,121
  • 9
  • 27
3

You can watch the event hidden.bs.collapse

see fiddle: http://jsfiddle.net/kyeuvx1d/

Kevin Friedheim
  • 314
  • 1
  • 9
1
if(!$("#id").hasClass('show')){
    alert("Uncollapsed");
}
else {
    alert("Collapsed");
}

for Bootstrap 4

sam007
  • 59
  • 6