Reputation: 541
I borrowed this code from somewhere to switch from one bootstrap modal to another. After the switch, I want to turn off the handler so it doesn't switch every time I simply close the first modal. My problem is that once the modal switching function is used, I don't know how to turn only that specific event handler off without turning off the other event I have firing when a certain modal closes (done elsewhere). Any suggestions?
//switch modals
function switchModals(fromModal, toModal) {
$(fromModal).on('hidden.bs.modal', function (e) {
$(toModal).modal('show');
//clear this function so it doesn't show up if they exit the window again
$(fromModal).off('hidden.bs.modal');
});
$(fromModal).modal('hide');
}
Upvotes: 1
Views: 203
Reputation: 541
I found a solution. I just had to give the handler a namespace that's unique to the function. Then I unbound it by its namespace in the ".off()" method. I chose "switch" as the namespace.
//switch modals
function switchModals(fromModal, toModal) {
$(fromModal).on('hidden.bs.modal.switch', function (e) {
$(toModal).modal('show');
//clear this function so it doesn't show up if they exit the window again
$(fromModal).off('.switch');
});
$(fromModal).modal('hide');
}
Upvotes: 0
Reputation: 32354
Try the following to toggle between modals:
var el = fromModal+','+toModal;
$(el).on('hidden.bs.modal', function (e) {
$(el).not(this).modal('show');
$(this).modal('hide');
});
Upvotes: 0
Reputation: 750
Try using this
instead of fromModal
inside the hidden.bs.modal
callback, e.g.
//switch modals
function switchModals(fromModal, toModal) {
$(fromModal).on('hidden.bs.modal', function (e) {
$(toModal).modal('show');
//clear this function so it doesn't show up if they exit the window again
$(this).off('hidden.bs.modal');
});
$(fromModal).modal('hide');
}
Upvotes: 0
Reputation: 479
You need to put this line outside the switchModals
$(<FromModalId>).modal('hide');
Upvotes: 1