muratkh muratkh - 6 months ago 14
Ajax Question

Iterating through jquery json

can anyone help i have an external json file which I can connect and show data manually but when I iterate by data.length (which is 100) it only shows the 100th data.

here is the code

var url ="output.json";
$.getJSON(url, function(data) {
for (var i=0;i <= data.length; i++) {
$('#stage').html('<h4>' + data[i].title + '</h4>');
}
});

Answer

I think it's because you're overwriting the #stage element with every iteration. Appending each data element might work better:

var url ="output.json";
$.getJSON(url, function(data) {
  for (var i=0;i <= data.length; i++) {
    $('#stage').append('<h4>' + data[i].title + '</h4>');
  }
});
Comments