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?

1
  • Is the translatinos spelling intentional, or is it a typo? Commented May 15, 2013 at 3:07

3 Answers 3

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!

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

2 Comments

This ways works - I has the same issue a few days ago - that's the way to go.
tried this an hour, but with another syntax... i'll try again ;)
1
jQuery('#bar').dialog({
   buttons : [
       {
        text: translations.ok,
        click: function(){}
       },
       {
        text: translations.cancel,
        click: function(){}
       },
   ]
});

Comments

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;
}

Comments

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.