jQuery UI Dialog Buttons from variables
Asked Answered
T

3

22

I have variables holding the translated labels for buttons inside a jquery ui dialog.

I cannot fill the button array key with the variable itself, and can't find any way to let it treat my variable just as string.

translations['ok'] = 'ok';
translatinos['cancel'] = 'cancel';

// not working
jQuery('#foo').dialog({
    buttons:
    {
        translations['ok']: function() { alert('foo-ok'); },
        translations['cancel']: function() { alert('foo-cancel'); }
    }
});

// working
jQuery('#bar').dialog({
    buttons:
    {
        "Ok": function() { alert('bar-ok'); },
        "Cancel": function() { alert('bar-cancel'); }
    }
});

Is there any way to get this to work with variable array keys?

Thulium answered 31/8, 2009 at 12:42 Comment(1)
Is the translatinos spelling intentional, or is it a typo?Venture
D
38

You can try this, may be it helps:

var buttonsOpts = {}
buttonsOpts[translations["ok"]] = function ....
buttonsOpts[translations["cancel"]] = function ....
jQuery('#bar').dialog({
   buttons : buttonsOpts
});

Hope it helps!

Departure answered 31/8, 2009 at 12:46 Comment(2)
This ways works - I has the same issue a few days ago - that's the way to go.Faso
tried this an hour, but with another syntax... i'll try again ;)Thulium
T
1
jQuery('#bar').dialog({
   buttons : [
       {
        text: translations.ok,
        click: function(){}
       },
       {
        text: translations.cancel,
        click: function(){}
       },
   ]
});
Therapy answered 2/4, 2014 at 12:42 Comment(0)
B
0

I know this is 4 years old, but it is the top result for an issue I have been having. Here was the results of my labor.

Simply call the function in a mouse or keyboard event, reference a function (without parentheses), define the buttons or set blank, set a title, and set the text to be displayed.

function confirmDialogue(fn, value, ok, cancel, title, text){
    if (typeof ok == "undefined" || ok == ""){ok = "Ok";}
    if (typeof cancel == "undefined" || cancel == ""){cancel = "Cancel";}
    var buttonsOpts = {};
    buttonsOpts[ok] = function() {fn(value);$( this ).dialog( "destroy" );}
    buttonsOpts[cancel] = function() {$( this ).dialog( "destroy" );}

    var NewDialog = $('<div id="dialogConfirm"><p>' + text + '</p></div>');
    NewDialog.dialog({
        title: title,
        dialogClass: "dialogue",
        modal: true,
        height: "auto",
        width: "auto",
        show: true,
        hide: true,
        close: function(){$(this).dialog('destroy');},
        buttons: buttonsOpts
    });
    return false;
}
Bog answered 22/8, 2014 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.