Nick
Nick

Reputation: 16105

Pass variable to jquery modal, how?

Here is my code for JQuery UI modal window:

$('<p>Message</p>').dialog({
    modal: true,
    buttons: {
      Ok: function() {
         $( this ).dialog( "close" );
      }
    }
});

How can I pass a variable instead of constant string (see Ok above)

Upvotes: 1

Views: 808

Answers (2)

Anas Nakawa
Anas Nakawa

Reputation: 2015

try this:

var dynamicButtons= {};
var buttonOne = 'Press Me Please!';
var buttonTwo = 'No way';
dynamicButtons[buttonOne] = function() {
    // do your stuffs
};
dynamicButtons[buttonTwo] = function() {
   // do your stuffs
};
$('<p>Message</p>').dialog({
    modal: true,
    buttons: dynamicButtons
});

demo: http://jsbin.com/omawu4/2/

Upvotes: 1

balexandre
balexandre

Reputation: 75113

from jQuery UI dialog button text as a variable

var button_name = 'Test';    
var dialog_buttons = {};

dialog_buttons[button_name] = function(){ 
    closeInstanceForm(Function); 
}    
dialog_buttons['Cancel'] = function(){ 
   $(this).dialog('close'); 
}

$('#instanceDialog').dialog({ 
    buttons: dialog_buttons 
});

Upvotes: 1

Related Questions