Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery modal dialog button text

Based on the code below to show a JQuery dialog, the button text shows up as the literal "b" as opposed to the value of the variable b.

Ie: showWarningDialog('myBody', 'myTitle', 'go') shows a dialog with a button labelled b instead of go.

How can you get go to appear?

function showWarningDialog(theBody, theTitle, buttonText) {
    var t = "Warning";
    if (theTitle != null) {
        t = theTitle;
    }

    var b = "Ok";
    if (buttonText != null) {
        b = buttonText;
    }

    $("#div-dialog-warning div").text(theBody);

    $("#div-dialog-warning").dialog({
        title: t,
        resizable: false,
        height: 160,
        modal: true,
        buttons: {
            b : function () {
                $(this).dialog("close");
            }
        }
    });
}
like image 540
Marcus Leon Avatar asked Oct 25 '10 15:10

Marcus Leon


2 Answers

As per the jQuery UI docs, the button name comes from the button's key in the buttons object. In which case, replace this bit:

$("#div-dialog-warning").dialog({
    title: t,
    resizable: false,
    height: 160,
    modal: true,
    buttons: {
        b : function () {
            $(this).dialog("close");
        }
    }
});

with this:

var buttonOpts = {};
buttonOpts[b] = function () {
    $(this).dialog("close");
};
$("#div-dialog-warning").dialog({
    title: t,
    resizable: false,
    height: 160,
    modal: true,
    buttons: buttonOpts
});

You have to treat b as a variable, hence using buttonOpts[b] rather than what you did, which is the equivalent of using buttonOpts.b.

like image 167
Matt Ball Avatar answered Nov 13 '22 11:11

Matt Ball


here's what you need to add afte ryou initialize your dialog:

 $('div.ui-dialog-buttonpane button:contains(b)').empty().html(b);

you'll probably want to rename b to be something a little more descriptive and unique though.

like image 38
Patricia Avatar answered Nov 13 '22 12:11

Patricia