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">×</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
Data can be passed to the modal body from the HTML document which gets displayed when the modal pops up. To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code.
To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.
The relatedTarget property returns the element related to the element that triggered the mouse event. The relatedTargert property can be used with the mouseover event to indicate the element the cursor just exited, or with the mouseout event to indicate the element the cursor just entered.
Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.
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" ) );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With