Dynamic button names in jQuery dialog

I have a webpage that uses different languages ​​stored in localStorage, and in the jQuery dialog I want the button names to be dynamically changed according to the language, for example:

var btn_hello_text = getLanguageBtnHelloText(); $('#dialog').dialog({ autoOpen: false, buttons: { btn_hello_text: function() { doThings(); } } }); 

The problem is that the button with the text "btn_hello_text" displayed in the dialog box, and not the value included in this variable. I can not understand how to dynamically change the value of the button text, any hints? Thanks.

+4
source share
2 answers

You can use parenthesis notation (instead of dot notation ), for example:

 var my_buttons = {}; my_buttons[getLanguageBtnHelloText()] = doThings; $('#dialog').dialog({ autoOpen: false, buttons: my_buttons }); 
+5
source

You cannot do this with an inline object declaration. But this can be done using the square bracket syntax instead:

 var btn_hello_text = getLanguageBtnHelloText(); var buttonDefs = {}; buttonDefs[btn_hello_text] = function() { doThings(); }; $('#dialog').dialog({ autoOpen: false, buttons: buttonDefs }); 
+1
source

All Articles