0

Javascript:

var json = {item1: {foo:bar},item2: {foo:bar)};

$('li').click(function() {
  var liID = $(this).attr('id'); // Outputs 'item1' or 'item2'
  var theFooValueIWant = json.liID.foo;
});

Pretty simple. I'm trying to get the value of the foo based off the ID of the <li> that gets clicked. But json.liID.foo looks for a liID in the json, which doesn't exist. How do I get it to look for json.the-value-of-liID instead of json.liID itself? Thanks!

1
  • 1
    Wow, I'm dumb! json[liID].foo! Of course! Commented Apr 29, 2011 at 19:04

2 Answers 2

4

var theFooValueIWant = json[liID].foo;

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

Comments

2

Use bracket notation:

var theFooValueIWant = json[liID].foo;

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.