1

I have problem with looping through JSON, my example prints only last block of JSON.

JSON example:

[{
"date": "May 15 2015 12:00:00:000AM",
"link": "somelink1.pdf"
}, {
"date": "May 15 2015 12:00:00:000AM",
"link": "somelink2.pdf"
}
]

jQuery part:

$.getJSON("PlaceOfMyJSON.php", function(response) {
$.each(response, function (i, item) {
$('#result').html(item.link);    
});
});

In live JSON I have many data but this loop print only last "link" field "somelink2.pdf" in my div with id result.

1 Answer 1

2

It is happening because you are overwriting your html again and again.

You need to update from

$('#result').html(item.link); 

to

$('#result').append(item.link); 

For reference - http://api.jquery.com/append/

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.