shown.bs.modal fires multiple times when you close and reopen modal

javascript, jquery, twitter-bootstrap, twitter-bootstrap-3

Solution

You need to extract your alert function out from your click event :

http://jsfiddle.net/SyCNj/2/

extract :

function openTestModal(){
    $('#testModal').modal({
        keyboard: false,
        backdrop: 'static'
    });
}

$('#testModal').on('shown.bs.modal', function (e) {
   alert('');
});

$('.testButton').click(function(){
    openTestModal();
});

Problem

I have made a fiddle illustrating the issue I am facing at the moment. So every time I close and open a modal, `shown.bs.modal` also fires multiple times. In this fiddle, every time you close and open a modal, number of alerts also increases (when it's supposed to trigger only once). http://jsfiddle.net/j36h2/1/ ``` function openTestModal(){ $('#testModal').modal({ keyboard: false, backdrop: 'static' }); $('#testModal').on('shown.bs.modal', function (e) { alert(''); }); } $('.testButton').click(function(){ openTestModal(); }); ```

Original source

Related problems