How to get the clicked element in Bootstrap collapse event?
Since my case featured multiple anchors next to eachother (viz. disregarding regular Bootstrap structure), I ended up searching for the clicked anchor by using the collapsing div's ID:
$('.collapse').on('show.bs.collapse', function (e) {
// Get clicked element that initiated the collapse...
clicked = $(document).find("[href='#" + $(e.target).attr('id') + "']")
});
This is working in Bootstrap 4.2
$(".collapse").on('shown.bs.collapse', function(e){
$clickedElement = $($(e.target).data('bs.collapse')._triggerArray);
});
This worked for me:
$('#collapse').on('shown.bs.collapse', function (e) {
// Get the clicked button element's text...
questionTitle = $(e.target).parent().find('button').text().trim();
console.log(questionTitle);
})
Try this
$element.on('shown.bs.collapse', function(e) {
var $clickedBtn = $(e.target).data('bs.collapse').$trigger;
});