You could try to use the .children
method to filter through your response – let’s say the posts are comming in containers with a particular selector on them – say, you add class="mypost"
on the divs when you build the response -, then you can do something like this in your success
method:
// ...
jQuery("#category-post-content").html(jQuery(response).children('.mypost'));
// ...
An alternative would be to do a foreach
loop through the $post
and build an array of $postElements
, then echo json_encode($postElements);
in your PHP functions file and parse it in the success
method, build the HTML output in there.
// ...
jQuery("#category-post-content").html(''); // Clean up old content
for (i in response) {
// Build the html string for each response[i] here
var postElement="<div id="post-" + response[i].id + '" class="' + response[i].classes '">'
+ .........;
// and then add it to your container
jQuery("#category-post-content").append(postElement);
}
// ...
It all depends how you want to approach this problem, but it’s a matter of best practice to not return HTML code directly in the server’s response to an AJAX request.