I'm trying to use Ajax GET URL to get the post content of three WordPress posts on the same domain. I have a single button and the URLs in an array in the script and it works, but all three URLs are loaded in a row quickly, I wanted to get each URL one at a time after each click and cycle through, so four clicks start at the beginning of the array again. This is my code:
<button type="button" class="loadAjax">Get Content</button>
<div id="target"></div>
<script>
(function ($) {
$('.loadAjax').click(function(){
var url = ['a-nice-post', 'a-small-gallery', 'a-nice-entry']
for (var i = 0; i < url.length; i++) {
var link = url[i];
$.ajax({
url: link,
type:'GET',
success: function(data) {
$('#target').html($(data).find('.entry-content').html());
}
});
}
});
})(jQuery);
</script>
Here is a link to my example when you click the "Get Content" button you will note that the beginning text of the content shows the URL, the rest is Lorem ipsum filler. Also note that it is slow to load the content so wait for a few seconds after clicking the button, I plan to add some feedback for the user after clicking, but I just wanted to keep the code here simple.