We're merging with GenerateBlocks! Learn more here.

[Resolved] Run jQuery after ajax load more

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Run jQuery after ajax load more

Tagged: , ,

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #5070
    Janek
    Participant

    Just wondering if theres a way to run a jQuery after the ajax loads more in the post list.

    At the moment I have a script that replaces the em dash with a line break and it doesn’t function correctly when I click load more posts. Assuming this is because the jQuery doesn’t fire again after page load.

    I found some instructions on stackoverflow which said you need to use something like the follow ajaxComplete function, but I still have no luck with it.

    jQuery(document).ajaxComplete(function() {
     jQuery('#wpsp-1023 .wp-show-posts-entry-title a').html(function(i, v) {
        return v.replace('–', '<br/>');
    }
    });
    

    Any assistance would be much appreciated. If you can think of a better way to have the hyphens replaced in the post titles I’m happy to do that.

    Thanks in advance!

    #5075
    Tom
    Keymaster

    Hey Janek,

    Not sure if this will work, but it’s worth a shot:

    jQuery( document ).on( 'wpsp_items_loaded', function() {
        jQuery('#wpsp-1023 .wp-show-posts-entry-title a').html(function(i, v) {
            return v.replace('–', '<br/>');
        }
    } );
    #5077
    Janek
    Participant

    EDIT: Deleted previous reply.

    It works. However it doesn’t seem to run on the initial titles before I click load more. It only applies to the new titles being loaded. Is there a way to get the code to work on the initial load as well? Or should I just duplicate the script and have one do the initial and another do the ajax loaded items?

    #5087
    Tom
    Keymaster

    This would be your full code:

    jQuery( document ).ready( function($) {
        $('#wpsp-1023 .wp-show-posts-entry-title a').html(function(i, v) {
            return v.replace('–', '<br/>');
        } );
    
        $( document ).on( 'wpsp_items_loaded', function() {
            $('#wpsp-1023 .wp-show-posts-entry-title a').html(function(i, v) {
                return v.replace('–', '<br/>');
            } );
        } );
    } );

    Let me know if that works or not 🙂

    #5088
    Janek
    Participant

    Seems to work perfectly! Thanks! Now to figure out why it works perfectly. 🙂

    #5093
    Tom
    Keymaster

    First block replaces them on page load, and second hooks into a trigger we call once the WPSP items have been loaded via AJAX 🙂

    #5095
    Janek
    Participant

    Makes sense! I might try to apply a trigger on another piece of script I’m running. Thanks!

    On a side note my notifications for this forum go to my spam folder on Gmail. Quite odd since I’ve specifically told it these notifications arent spam.

    #5107
    Tom
    Keymaster

    That’s strange – I’ll look into that.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.