27

Button invoked modal dialog: When button is clicked, event is fired the resulting event reference e.relatedTarget is undefined. So, how can I get the invoking button from the handler? e does not seem to contain any reference to the invoking button.

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

jQuery:

$('#myModal').on('show.bs.modal', function (e) {
  console.log(e.relatedTarget) // do something...
})

Reference: http://getbootstrap.com/javascript/#modals

jgillich
  • 71,459
  • 6
  • 57
  • 85
eyn
  • 778
  • 2
  • 10
  • 21

6 Answers6

26

Thanks Geo1004.. I was already using JS to trigger the "show" event on the modal, but I was missing the second argument. So the event.relatedTarget was undefined.

If anyone else is going the JS route (instead of using data attributes) I would make sure you are sending the button as a jQuery object -

$( "#myModal" ).modal( "show", $( "#buttonBeingClicked" ) );
JDev518
  • 772
  • 7
  • 16
22

Take a look at the following Bootply example.

When run the show event seems to include a proper reference to e.relatedTarget.

$('#myModal').on('show.bs.modal', function (e) {
    var button = e.relatedTarget;
    if (button != null)
    {
        alert("Launch Button ID='" + button.id + "'");
    }
})

Take a look at the Bootply example to see if your own code deviates from it. (I copied the original Bootstrap sample code snippet directly from the link that you provided.)

I hope this helps. Good luck.

László Koller
  • 1,139
  • 6
  • 15
  • you must have taken your bootply example away because I can't find it. – eyn Aug 25 '14 at 19:02
  • The link (http://www.bootply.com/yPkQNLIqeI) still appears to work. Be sure to press the "Launch demo modal" button in order to run the demo code. – László Koller Aug 26 '14 at 04:44
  • 4
    Is the example at [link](http://www.bootply.com/yPkQNLIqeI) working as intended for question purpose? I mean that I never get the message from the alert related with e.relatedTarget. That object seems to be always NULL. – Delmo Mar 13 '15 at 04:53
10

This is a workaround. You can open the modal with JS instead and relatedTarget will be set. You should not miss to pass the button as a second parameter for the 'modal' method.

You will be able to pass data references (here address-target)

$('.listing_btn').on 'click', ->
  $('.modal').modal('show', $(@))

$('#modal').on 'show.bs.modal', (event)->
   button = $(event.relatedTarget)
   address_id = button.data('address-target')
   console.log address_id 
geo1004
  • 101
  • 1
  • 4
4

I had the same problem and wasted the whole evening to fix it. My bootstrap.js version was simply to old and the $(event.relatedTarget) was not available for the 'show.bs.modal' event. Anyone with the same problem should check his bootstrap version first. Then simply stick to the official site example : http://getbootstrap.com/javascript/#modals-related-target You'll find your reference in $(event.relatedTarget).

Hector
  • 101
  • 1
  • 4
2
$('#myModal').on('show.bs.modal', function (e) {
   console.log($(e.relatedTarget)); // You will get the element you want! Cheers
})
Akshay Bajpei
  • 196
  • 2
  • 9
0
$('#myModal').modal('show',$('#myButton'))

$('#myModal').on('show.bs.modal',function(e){
   console.log($(e.relatedTarget))
})