1

Possible Duplicate:
How to access object using dynamic key?

I have multiple select with different options, the later options depend on the earlier choices. So I would like to generate options for later select, I am in the middle of doing this, and I have encountered some problems:

function generateOptions(selected) 
  { 
  var jsonObj = ('food':['a','b','c'])
  //able to get selected_value as food  
  var selected_value = selected.options[selected.selectedIndex].value 
  var options = jsonObj.selected_value  
  } 

if I do jsonObj.food, then I can get output in console ['a','b','c']. But if I try to use selected_value, then I get undefined

So the . for retrieving data from an Object only works for absolute values not a var? Anybody can help?

1
  • var jsonObj = ('food':['a','b','c']) doesn't look like valid JavaScript btw. Commented Mar 14, 2012 at 20:00

1 Answer 1

4

Use bracket notation for dynamic property names:

var options = jsonObj[selected_value];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.