2

I am trying to loop through a simple JSON array and display the contents with jQuery. My JSON data is:

 {
   "cards":[
      {
         "title":"cat",
         "spanishWord":"gato"
      },
      {
         "title":"dog",
         "spanishWord":"perro"
      }
   ]
}

Here is the jQuery I am using:

    var jqxhr = $.getJSON("http://www.myurl.com/cards.js", function (data) {

        $.each(data.cards, function (i, item) {
            $(".list").append("<li id='" + cards[i].title + "'>" + cards[i].title + cards[i].spanishWord + "</li>");
        });
    });

I am pretty certain the problem is in my each statement but I can't figure out what is wrong.

1
  • item.title will be cleaner Commented Oct 6, 2013 at 16:37

1 Answer 1

4

The problem is inside the loop, where you use cards[i] instead of data.cards[i].

You could also use item instead of data.cards[i].

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

2 Comments

Perfect, thanks! I'll accept your answer when the time limit is up.
Be sure to use item instead of data.cards[i]. Much cleaner!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.