tags:

views:

30

answers:

1

Using the code from the jqModal website, section FUN! Overrides:
http://dev.iceburg.net/jquery/jqModal/#examples
(I edited the code to make use of a callback function)

function confirm(msg,callback) {
  $('#confirm')
    .jqmShow()
    .find('p.jqmConfirmMsg')
      .html(msg)
    .end()
    .find(':submit:visible')
      .click(function(){
        if(this.value == 'yes')
          (typeof callback == 'string') ?
            window.location.href = callback :
            callback();
        $('#confirm').jqmHide();
      });
}

$().ready(function() {
  $('#confirm').jqm({overlay: 88, modal: true, trigger: false});

  // trigger a confirm whenever links of class alert are pressed.
  $('a.confirm').click(function() { 
    confirm('About to visit: '+this.href+' !',callbackfunction); 
    return false;
  });
});

function callbackfunction()
{
 console.log("callback triggered");
}

The problem: each time when the confirm function is called, the callback gets triggered incrementally, so when I click the 2nd time, the handler gets executed 2 times, the 3th time, 3 times and so on.

A: 

This happens because each time you call the confirm function, you add a new 'click' event handler.

sje397
i see :), but i dont really know how to change the code to make it work like it should. I have multiple confirm dialogs that should call different callbacks
Sobek
You just need to set them up once, e.g. on document ready, then just change the message text and show when you call e.g. the confirm method. Or, set a boolean to indicate that you've already added the handler, and don't add it again.
sje397
yes, indeed but when i define the click handler on document ready level, the callbackfunction() that would be executed is not variable, unless i define a variable specific for this that is changed when the click handler of the anchor is called.
Sobek
anyway thanks for your help :)
Sobek