Conditional button added to jQuery modal
jquery, modal-dialog
Solution
You could create the `buttons` object before creating the dialog:
//Create the buttons object
var buttons = {
Ok: function() {},
'A Button': function() {}
};
//Add another button to that object if some condition is true
if(something) {
buttons['B button'] = function() {};
}
//Create the dialog, passing in the existing buttons object
$("#dialog").html("<div></div>").dialog({
buttons: buttons,
//Other options
});
Problem
I have a jQuery modal where I want to add an additional button if a conditional statement is met. Original sample code (cutdown): ``` $("#dialog").html("<div></div>").dialog({ title: "Some Title", modal: true, width: 550, buttons: { Ok: function() { // }, 'A Button': function() { // } } }).dialog('open'); ``` So as you see above there is a modal with 2 buttons, but I also want to add in there some dynamic code to be able to cater for an additional button if a variable is set. e.g. ``` var some_variable = 0; $("#dialog").html("<div></div>").dialog({ title: "Some Title", modal: true, width: 550, buttons: { Ok: function() { // }, 'A Button': function() { // } /* ??? */ if (some_variable==1) //then add the other button's code here.. /* ??? */ } }).dialog('open'); ```