0

I'm using moment.js for local time formatting. It's working fine with initially loaded elements but when the elements are loaded via ajax, the time formatting (moment.js) is not working.

My initializing code for moment.js:

$(function () {
    var htmlLang = $('html')[0].lang;
    moment.locale(htmlLang);
    $( "[data-time-format]" ).each(function() {
        var el = $( this );
        switch(el.attr("data-time-format")) {
            case "time-ago":
                var timeValue = el.attr("data-time-value");
                var timeAgoStr = moment.utc(timeValue).fromNow();
                el.text(timeAgoStr);
                break;
        }
    });
});

my ajax loader would look like:

$.ajax({
        url: "/path/loadData.php",
        type: "POST",
        data: { jsondata:jsondata },
        dataType: "html",
        success: function(response) {
          $("container").append(response);
          //I guess I have to reinitialize moment.js
        },
        error: function() {
          alert("Data not loaded.");
        }
      });
Bipul Roy
  • 506
  • 2
  • 7
  • 18

0 Answers0