2

Upon opening a jQueryUI Dialog, I would like to perform a GET request, and based on the response, change the button text. After hours of struggling, I finally got the following working. Is this really the best/only? Thanks

$("#dialog").dialog({
    open        : function() {
        var dialog=$(this);
        $.get('ajax.php', function (data) {
            var buttons=dialog.dialog( "option", "buttons" );
            buttons[1].text=(data==1)?"CANCEL":"CLOSE";
            var buttons=dialog.dialog( "option", "buttons" ,buttons);
        });
    },
    buttons     : [
        {
            text    : 'SAVE',
            click    : function() {}
        },
        {
            text    : 'CANCEL',
            click    : function() {}
        }
    ]    
});
2
  • why you dont make your ajax.php call first and open the dialog after the response? Commented Nov 29, 2012 at 13:30
  • @silly. What would you change after the ajax call? What about the way I am copying the entire button object, and reinitializing the dialog with it? Commented Nov 29, 2012 at 13:34

1 Answer 1

2

You can change the button text like this...

the below given is without ajax

function setbutton(button1, button2) {
var btns = {};
btns[button1] = function() {
    //your function
   $( this ).dialog( "close" );
};
btns[button2] = function() {
    // Do nothing
    //your function
    $( this ).dialog( "close" );
};

document.getElementById('dialogshow').innerHTML = "<div>open with given button text</div>";


$( "#dialogshow" ).dialog({
    autoOpen: true,
    width: 450,
    height: 200,
    modal: true,
    position: 'center',
    modal: true,
    buttons: btns
});
}
$('.test').click(function() {
setbutton('start', 'End');//in here button name you want..
});

See the Live Demo

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

1 Comment

Thanks Dinesh. Interesting approach. It seems cleaner than my approach.

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.