javascript - Hide Load more Ajax button if there is no more users to load or less than the number?

admin2025-06-03  6

How to hide the Load more Ajax button if there is no more users to load or less than the number

<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
    $('body').on('click', '.loadmorefollowing', function() {
            var data = 
            {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author': '<?php echo esc_html($curauth->ID); ?>'

            };

    $.post(ajaxurl, data, function(response) {

        $('#following-artists-cont').append(response);
        page++;
    });
    });
});
</script>

How to hide the Load more Ajax button if there is no more users to load or less than the number

<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
    $('body').on('click', '.loadmorefollowing', function() {
            var data = 
            {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author': '<?php echo esc_html($curauth->ID); ?>'

            };

    $.post(ajaxurl, data, function(response) {

        $('#following-artists-cont').append(response);
        page++;
    });
    });
});
</script>
Share Improve this question asked Feb 8, 2019 at 19:52 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I think something like this may work, if response is just HTML and returns an empty string '' when there are no more results:

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
    var page = 2;
    jQuery(function($) {
        $('body').on('click', '.loadmorefollowing', function() {
            var data = 
                {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author': '<?php echo esc_html($curauth->ID); ?>'

                };

            $.post(ajaxurl, data, function(response) {
                if ( '' === response ) {
                    $('.loadmorefollowing').hide();
                    return;
                }

                $('#following-artists-cont').append(response);
                page++;
            });
        });
    });
</script>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748930184a314912.html

最新回复(0)