0

I've a Modal which I show for confirmation:

<div class="modal" tabindex="-1" role="dialog" id="modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
             ... Body ...
            <div class="modal-footer">
                <button type="button" id="btnCancel" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" id="btnOk" class="btn btn-primary" data-dismiss="modal">Ok</button>
            </div>
        </div>
    </div>
</div>

Here is the code for showing the above Modal..

var M = {
 confirm: function(body, yes, no) {
    $('#btnCancel').on('click', function() {
       $('#btnCancel').off();
       $('#confirm-modal').modal('hide');
       if (no) {
            return no();
       }
    });
    $('#btnOk').on('click', function() {
      $('#btnOk').off(); 
      $('#modal').modal('hide');
      if (yes) {
         return yes();
      }
    });
  }
}

And here is how I am using it... (In any view for example)

M.confirm("Body", function(){
   // Function Body
})

Now, the problem is: If I call confirm, it shows the Modal. But if I click Cancel and than again call confirm and this time, I hit OK: the function (second param in the snippet above) is called twice. If I hit cancel 10 times, the function above is called 10 times.

Any idea why this is happening?

Thank You.

5
  • You are assigning event handlers every time you call confirm(). So the second time you call confirm, you are adding another set of handlers (not replacing the old ones). You should remove the event handlers from the confirm() function and just assign them once. Commented Apr 21, 2021 at 14:41
  • Consider using .one() api.jquery.com/one Commented Apr 21, 2021 at 14:42
  • 1
    .on() is additive... .one() is only once. Commented Apr 21, 2021 at 14:45
  • @Scott/Hive7/Jeremy Got it.. Thank you all.. Commented Apr 21, 2021 at 14:57
  • 1
    @JeremyThille That's a bit misleading since you can add multiple listeners with one() but each only gets called once. jsfiddle.net/oubm4vtx Commented Apr 21, 2021 at 14:57

1 Answer 1

1

Every time you call confirm you add an event handler to each of btnCancel and btnOk.

However, you only remove the effect handler on one of those buttons when that button is clicked.

So if you call confirm, then click btnCancel, then call confirm you'll have two event handlers on btnOK: One from the first call to confirm and once from the second.

When either button is clicked you need to remove both event handlers.

… or just add the event handlers when the document is loaded and don't touch them thereafter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.