Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap jquery show.bs.modal event won't fire

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.

like image 552
m4rlo Avatar asked Oct 09 '13 18:10

m4rlo


People also ask

How do I display a modal popup?

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.

Does Bootstrap modal require jQuery?

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.

What is modal show in jQuery?

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') .


2 Answers

use this:

$(document).on('show.bs.modal','#myModal', function () {   alert('hi'); }) 
like image 153
Ali Avatar answered Oct 10 '22 17:10

Ali


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.

like image 35
Pierre Avatar answered Oct 10 '22 19:10

Pierre