i'm using the modal example from the bootstrap 3 docs. the modal works. however i need to access the show.bs.modal event when it fires. for now i'm just trying:
$('#myModal').on('show.bs.modal', function () { alert('hi') })
Nothing happens, the event does not fire. What am I doing wrong??? This doesn't make sense to me.
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.
Bootstrap 5 is designed to be used without jQuery, but it's still possible to use our components with jQuery. If Bootstrap detects jQuery in the window object it'll add all of our components in jQuery's plugin system; this means you'll be able to do $('[data-bs-toggle="tooltip"]'). tooltip() to enable tooltips.
Answer: Use the modal('show') Method You can simply use the modal('show') method to show or open the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('hide') and modal('toggle') .
use this:
$(document).on('show.bs.modal','#myModal', function () { alert('hi'); })
Make sure you put your on('shown.bs.modal')
before instantiating the modal to pop up
$("#myModal").on("shown.bs.modal", function () { alert('Hi'); }); $("#myModal").modal('show'); //This can also be $("#myModal").modal({ show: true });
or
$("#myModal").on("shown.bs.modal", function () { alert('Hi'); }).modal('show');
To focus on a field, it is better to use the shown.bs.modal
in stead of show.bs.modal
but maybe for other reasons you want to hide something the the background or set something right before the modal starts showing, use the show.bs.modal
function.
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