2

When an object is clicked on a page I am collecting the data from that object and I need to pass that into a jQuery each loop but substitute my JSON object for the data I have.

Here is my HTML

<a href="#">Large</a>

Here is my JSON Data

var sizeArray = {"Large":{"Red":[78],"Blue":[0],"Green":[0]}

And finally the JS I'm having a problem with

var size = $('a').text();
$.each(sizeArray.size, function(key, val){    
    if(this == 0){
        alert(key);
}});

All I'm getting is an error saying "a is undefined" in jQuery. I know the issue is not with jQuery and the issue is with the par that says "sizeArray.size". If I replace the word size with Large than everything works. I just can't figure out how to pass that in!

1
  • you are missing one } at the end of sizeArray definition, but thats probably not the problem Commented Feb 22, 2011 at 1:53

1 Answer 1

7

Try using:

$.each(sizeArray[size], function(key, val) { ... });

instead.

For more information, check out "Objects as associative arrays"

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

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.