I have something like :
var myMenu= [
{
'My English Title':function(menuItem,menu)
{ ... }
}];
Now, I want the replace 'My English Title' by an other JSON structure value LOC.MENU_TITLE
.
I have try:
var myMenu= [
{
LOC.MENU_TITLE:function(menuItem,menu)
{ ... }
}];
But it doesn't work. Any one can give me an hint of how to get JSON value inside a JSON variable?
Edit: This is to use the Context Menu of Jquery but I got a problem with all solutions below and it's when it's time to pass the String for the Separator. It doesn't show the separator because it pass the string into an object instead of just the string.
Example:
var menu1 = [
{'Option 1':function(menuItem,menu) { alert("You clicked Option 1!"); } },
$.contextMenu.separator,
{'Option 2':function(menuItem,menu) { alert("You clicked Option 2!"); } }
];
Use bracket notation to get an object's value by a variable key, e.g. obj[myVar] . The variable or expression in the brackets gets evaluated, so if a key with the computed name exists, you will get the corresponding value back. Copied!
JavaScript object key names must adhere to some restrictions to be valid. Key names must either be strings or valid identifier or variable names (i.e. special characters such as - are not allowed in key names that are not strings).
var tempElement = {};
tempElement[ LOC.MENU_TITLE ] = function(menuItem, menu) {};
myMenu = [ tempElement ];
Addressing your edit. Try the following
var myMenu = [ {}, $.contextMenu.separator, {} ];
myMenu[0][ LOC.MENU_TITLE ] = function(menuItem, menu) {};
myMenu[2][ LOC.MENU_TITLE2 ] = function(menuItem, menu) {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With