FrancescoLorenzetti84 FrancescoLorenzetti84 - 2 months ago 6
jQuery Question

Get the element whose click opened a Bootstrap Modal dialog

How do I know which button triggered the opening of a Bootstrap Modal Dialog?

<a href="#" data-toggle="modal" data-target="#myModal">
Button 1
</a>
<a href="#" data-toggle="modal" data-target="#myModal">
Button 2
</a>


Javascript

$('#myModal').on('shown.bs.modal', function () {
var triggerElement = ???
})

tmg tmg
Answer

Its documented here

$('#myModal').on('shown.bs.modal', function (event) {
     var triggerElement = $(event.relatedTarget); // Button that triggered the modal
});