mohamnag mohamnag - 7 months ago 70
Javascript Question

Trigger a button click inside a jQuery UI Dialog

it is a very simple question that I'm not finding an answer for it. I have a dialog and in some events happening inside the dialog I want to click one of the dialog buttons. The code which defines the dialog is:

var dialog = $('<div>').dialog({
autoOpen: false,
title : title,
resizable : false,
buttons : {
'CANCEL' : {
text : messages.Cancel,
click : function(){$(this).dialog('close')}
},
'OK' : {
text : messages.Ok,
click : okButtonCallback
}
}
});


and in my event I can get the dialog, find the buttons but I can not trigger the click event with right reference passed as this. I do this:

buttons = dialog.dialog('option', 'buttons');


and I have the buttons each of them has the click function. If called directly or through trigger('click'), they call the click event of button but with the button itself as this not the dialog object.
I saw somewhere to call

buttons['OK'].apply(dialog);


but my buttons have absolutely no apply function!

I'm not sure what can I do!

Answer

First of all, you need to get buttons[0] not buttons['OK'], then, it's not a function it's an object, try to get to click function like this :

buttons[0].click.apply(dialog);
Comments