John
John

Reputation: 1319

JQUERY LIVE Confirm issue

My issue is relating to dynamically applied elements to a page that I am running a confirm against. The code correctly targets the element and asks the confirmation part, however the problem is if I select Yes, I have no idea where the return value is hidden, it is not set in the class like my thoughts below, nor does it return a value of sorts.

Anyone know how I can get some form of return value from the confirm for the element that I wish to delete?

    $('.deleteMe').live('click', function() {

        confirmFunction(this);
        if ($(this).hasClass('confirm')){ alert("yea");} else {alert("no");}
    });

    function confirmFunction(element) {
        $(element).confirm({
          msg:'Confirm Action!<br />',
          stopAfter:'ok',
          eventType:'click',
          timeout:5000,
          buttons: { ok:'Yes', cancel:'No', separator:' - '}
        });
    }

Upvotes: 0

Views: 498

Answers (1)

Fosco
Fosco

Reputation: 38526

From my understanding of the examples on the plug-in page, it should be like this:

Assign the click handler that should run if confirmation is successful, then assign the confirm plugin.

 $('.deleteMe').live('click',function() {
    // Code here for when they click YES on the confirm box.
    // This only executes in 2 scenarios, either the item was clicked
    //   and the confirm plugin was not active, or it was active and
    //   the user selected Yes.
 });

 function updateConfirms() {
    $('.deleteMe').confirm({
      msg:'Confirm Action!<br />',
      stopAfter:'ok',
      eventType:'click',
      timeout:5000,
      buttons: { ok:'Yes', cancel:'No', separator:' - '}
    });
 }

 updateConfirms();

Due to the dynamic nature you mentioned, you would need to call updateConfirms() after adding new elements that need confirmations.

EDIT: Let's try not using Live, and refreshing the click handler and the confirm after any additions:

 function updateConfirms() {
   $('.deleteMe').click(function() {
    // Code here for when they click YES on the confirm box.
    // This only executes in 2 scenarios, either the item was clicked
    //   and the confirm plugin was not active, or it was active and
    //   the user selected Yes.
   });

   $('.deleteMe').confirm({
     msg:'Confirm Action!<br />',
     stopAfter:'ok',
     eventType:'click',
     timeout:5000,
     buttons: { ok:'Yes', cancel:'No', separator:' - '}
   });
 }

 updateConfirms();

Upvotes: 1

Related Questions