Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 687
mohamnag Avatar asked Sep 23 '11 14:09

mohamnag


2 Answers

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);
like image 111
Shlomi Komemi Avatar answered Sep 30 '22 00:09

Shlomi Komemi


$('.ui-button:contains("Ok")').click()
like image 25
aziz punjani Avatar answered Sep 30 '22 00:09

aziz punjani