Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Get .timeago() to work on elements loaded after DOM ready (Ajax)

Using pageless1 to trigger an ajax load of older, timestamped content onto the end of the page, I need .timeago() to act upon those newly loaded dates. It works on the content loaded the first time you arrive at the page, but not the content loaded via Ajax later.

Thanks!

like image 316
Alfonso Avatar asked Aug 05 '10 13:08

Alfonso


1 Answers

For running plugins (things not event handler based) you can still use the .livequery() plugin, like this:

$('.selector').livequery(function() {
  $(this).timeago();
});

If it's an event handler, .live() or .delegate() works, or if it's a plugin you can also run the plugins on loaded content, for example:

$.ajax({
  //options...
  success: function(data) {
    //do stuff
    $('.selector', data).timeago();
  }
});

This runs the plugin on that selector, but only for elements that came back in that AJAX response, by using the context argument of $(selector, [context]).

like image 155
Nick Craver Avatar answered Oct 15 '22 13:10

Nick Craver