Load More Posts Button – AJAX

After a chat session between trunks (the OP) and I, here’s what we discerned the issue was.

In success within the AJAX there was a chained series of instructions. .text().prev().before();

Once the button was added into a div, this presented itself as a problem because the new posts were appearing before the button, but the button was changing, etc.

So instead, this is what works to address it, separating the actions taken and targeting both the parent div and the button. (This is just the ‘success’ portion of the AJAX, the rest is all valid as it was.)

success : function( data ){
    if( data ) {
        //this loads the new posts from the ajax callback
        $( '.show-more' ).prev().before(data);
        //this modifies the button
        button.text( 'More posts' ); 
        $('.loadmore').removeClass('newcomment');
        loadmore_params.current_page++;
        if ( loadmore_params.current_page == loadmore_params.max_page ) 
        button.remove(); 
    } else {
        button.remove();
    }
}