简体   繁体   中英

How to fire 2 events together for bootstrap modal?

I have below 2 events for bootstrap modal

https://getbootstrap.com/docs/4.0/components/modal/

$('.modal').on('show.bs.modal', function() {

});

$('.modal').on('hidden.bs.modal', function() {

}); 

I have same code in both events.

Can I call both together at one time only for code optimisation.

You can store your function in a variable.

var fnCallback = function() {
  //my function
  console.log('my function was triggered');
};

$('.modal').on('show.bs.modal', fnCallback);
$('.modal').on('hidden.bs.modal', fnCallback); 

Yes, you can do:

$('.modal').on('show.bs.modal hidden.bs.modal', function() {

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM