views:

616

answers:

1

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?

+7  A: 

You cat try this, may be it helps:

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

Hope it helps! Sorry i can`t test it now.

Alexey Ogarkov
This ways works - I has the same issue a few days ago - that's the way to go.
Emil Ivanov
tried this an hour, but with another syntax... i'll try again ;)
Karsten
ok, with your syntax this works fine now ;) thank you
Karsten
Thanks this helped
Munim Abdul
+1. Precisely what I was after !
FreekOne