views:

40

answers:

4

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");
            }
        }
    });
}
+3  A: 

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.

Matt Ball
A: 

{b: 'blah'} wil mean it takes b as a variable name. defining the array by hand could fix it although i cant imagine there isn't a special syntax for that var buttons = {}; buttons[b] = function() { }; $().dialog({buttons: buttons});

DoXicK
A: 

I think you have missing parameter in the congig array. There should be something like

buttons: {
        b : function () {
            $(this).dialog("close");
        },
        buttonText: b
}
infinity
There is no `buttonText` option. As per the docs, `The property key is the text of the button`.
Matt Ball
A: 

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.

Patricia