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!

link|improve this question

60% accept rate
you are missing one } at the end of sizeArray definition, but thats probably not the problem – ZolaKt Feb 22 '11 at 1:53
feedback

1 Answer

up vote 7 down vote accepted

Try using:

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

instead.

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

link|improve this answer
perfect thanks! – timWhit Feb 22 '11 at 3:08
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.