0

I'd like to pass a variable to a Jquery Ui dialog confirmation box to do something when press YES and only close the dialog when press No. Now I have this code

$("#confirmDialog" ).dialog({
resizable: false,
autoOpen: false,
modal: true,
buttons: {
    'OK': function() {
        $( this ).dialog( "close" );
        },
    'Cancel': function() {
        $( this ).dialog( "close" );
        }
    }
 });


$(".confirm").click(function(){

var myHref = $(this).attr("href");

   $("#confirmDialog").dialog( "open" ).html ( "Confirm?" )
   $("#confirmDialog").on( "dialogclose", function( event, ui ) { window.location = myHref } );
   return false;
});

For now I found only the function on("dialogclose") and nothing else. How can I set the ui dialog to do something when the user press YES? I tried to pass the variable as well but without success

$(".confirm").click(function(){    
$("#confirmDialog").attr(myHref).dialog( "open" ).html ( "Confirm?" )
return false;
}); 

$("#confirmDialog" ).dialog({
resizable: false,
autoOpen: false,
modal: true,
buttons: {
    'OK': function() {
        window.location.href = myHref;
        $( this ).dialog( "close" );
        },
    'Cancel': function() {
        $( this ).dialog( "close" );
        }
    }
     });

or

$("#confirmDialog").dialog( "open" ).html ( "Confirm?" )
$("#confirmDialog").on( "OK", function( event, ui ) { window.location = myHref } );

how can I do this? Thanks!

1
  • You would have to write an extension method. What are you trying to achieve? Do you want to set some value when user clicks ok inside the dialog? Commented Feb 17, 2013 at 11:33

1 Answer 1

1

You could write it so your ok button click (inside the dialog), calls a function which uses the variable you want to set. You then set that variable inside the link click event, then display the dialog for confirmation.

For example:

    var $dialog = $( '#dialog-confirm' );
var $button = $('#linkButton');
var link = '';

// init dialog

$dialog.dialog({
  resizable: false,
  height:340,
  modal: true,
  autoOpen: false,
  buttons: {
    Ok: function() {
      doSomething(); // reference your function here
      $( this ).dialog( "close" );
    },
    Cancel: function() {
      $( this ).dialog( "close" );
    }
  }
});

// bind event

$button.on('click', function(e){
  e.preventDefault();
  link = $(this).attr('href');
  $dialog.dialog('open');
});

function doSomething(){
  window.location = link;
}

Working example here: http://jsbin.com/azezal/2/

Code view here: http://jsbin.com/azezal/2/edit

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

2 Comments

thank but I would like to use a link not a button, and I would like to use the href attribute to set my link variable. I tried so $(".del").click(function(){ link = $(this).attr("href"); $dialog.dialog('open'); }); but does not work :(
The code is actually very similar, I have updated my answer. Hopefully that's right :)

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.