Reputation:
I am trying to create a loop from the json data of an ajax respone. The json returned is:
{
status: "ok",
count: 2,
count_total: 2,
pages: 1,
posts: [
{
id: 19,
content: "<p>Dektop Test 2</p> ",
},
{
id: 11,
content: "<p>Desktop Test</p> ",
}
]
}
I using the following code to convert the json data into html markup, my problem is getting a loop going. I would like to output markup for each instance of data
<script>
jQuery(function($) {
$.getJSON('theurlforjson')
.success(function(response) {
var $title = $('.content').html(response.posts[0].content);
});
});
</script>
<div class="content"></div>
You can see the the json data for .posts[0].content is loaded into .content, I would like some help creating a loop with the scenario
Upvotes: 0
Views: 253
Reputation: 15616
Using:
for( var post in response.posts ){
$('.content').append( response.posts[post].content );
}
Check out the MDN Article on the for..in syntax. And caching the $('.content')
selector in a variable will improve the performance.
Upvotes: 2