Klaus Nji
Klaus Nji

Reputation: 18857

jquery: undefined value when accessing item on json array

Trying to learn jquery here so I took a regular javascript snippet that loops through a json collection like this:

 for (var i in msg) {
                        alert(msg[i].Name);
                    }

In this case, the alert box displays the correct name.

However, if I use jquery like this:

  $.each(msg, function(item) {
                        alert(item.Name);
                    });

alert box show undefined for each item in the json collection. What did I miss?

TIA.

Upvotes: 0

Views: 490

Answers (1)

user1106925
user1106925

Reputation:

Use the second parameter.

  // this one-------------v
$.each(msg, function(i, item) {
    alert(item.Name);
});

Upvotes: 2

Related Questions