javascriptjqueryhtmltimeago

timeago.js timeago function inline


I have this time element that is created dynamically.

<time class="timeago" datetime="/*Current Time*/"></time>

So i have been using this script:

$(document).ready(function() {
   $('.timeago').timeago();
});

As the element is created dynamically, the document.ready function doesnt seem to work on it. My question is, is there anyway to add the timeago function inline like this:

<time class="timeago" datetime="/*Current Time*/" onload="timeago();"></time>

or a document.ready function for dynamically created elements?

Thanks in advance.


Solution

  • Since you are adding the element dynamically you need to trigger timeago() once the element is added.

    Change your script to

    $('.div').append('<time class="timeago" datetime="/*Current Time*/"></time>');
    $(".div .timeago").timeago();