Home › Forums › Pro Support › Run jQuery after ajax load more
- This topic has 7 replies, 2 voices, and was last updated 4 years, 11 months ago by
Tom.
-
AuthorPosts
-
June 29, 2018 at 6:16 am #5070
Janek
ParticipantJust 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!
June 29, 2018 at 8:56 pm #5075Tom
KeymasterHey 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/>'); } } );
June 30, 2018 at 6:35 pm #5077Janek
ParticipantEDIT: 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?
July 1, 2018 at 10:36 pm #5087Tom
KeymasterThis 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 🙂
July 1, 2018 at 11:52 pm #5088Janek
ParticipantSeems to work perfectly! Thanks! Now to figure out why it works perfectly. 🙂
July 2, 2018 at 7:53 pm #5093Tom
KeymasterFirst block replaces them on page load, and second hooks into a trigger we call once the WPSP items have been loaded via AJAX 🙂
July 2, 2018 at 9:20 pm #5095Janek
ParticipantMakes 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.
July 3, 2018 at 7:05 pm #5107Tom
KeymasterThat’s strange – I’ll look into that.
-
AuthorPosts
- You must be logged in to reply to this topic.