Bootstrap modal dialog, show.bs.modal event relatedTarget is undefined. How can I get clicked element?
Asked Answered
P

6

28

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

Ppm answered 27/5, 2014 at 13:57 Comment(3)
This seems to be an issue with Bootstrap v3.2.0 and not an issue with some later versions (v3.1.1).Biogenesis
@Biogenesis Is there an issue report on the BS 3.2.2 GitHub page about this issue somewhere?Pyriform
try this answers, it work for me: https://mcmap.net/q/503411/-bootstrap-modal-relatedtarget-is-undefinedMahmoud
L
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" ) );
Laddie answered 17/8, 2015 at 20:14 Comment(4)
oowww save my life!! tksPercolator
This works for me with Bootstrap 3.3.5 and jQuery 2.x.Commingle
How do you use it with .modal({show:true,}); ?Gangboard
I believe show: true is implied. true to open the modal and false to close it. Are you looking to pass it additional options?Laddie
D
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.

Dextrocular answered 27/5, 2014 at 17:56 Comment(3)
you must have taken your bootply example away because I can't find it.Ppm
The link (bootply.com/yPkQNLIqeI) still appears to work. Be sure to press the "Launch demo modal" button in order to run the demo code.Wernher
Is the example at link 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.Hegelian
C
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 
Concordat answered 17/8, 2015 at 16:31 Comment(0)
F
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).

Fixed answered 26/7, 2015 at 23:33 Comment(1)
Updated from 3.3.5 to 3.3.7 ... didn't work, am using .load() to get modal data.Gangboard
T
2
$('#myModal').on('show.bs.modal', function (e) {
   console.log($(e.relatedTarget)); // You will get the element you want! Cheers
})
Tagore answered 1/5, 2017 at 11:19 Comment(0)
B
0
$('#myModal').modal('show',$('#myButton'))

$('#myModal').on('show.bs.modal',function(e){
   console.log($(e.relatedTarget))
})
Barns answered 10/5, 2022 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.