-1
   $.ajax({
            url: '/Staff/GetStaffFamilyDetails',
            type: 'POST',
            data: {
                StaffID: StaffID
            },
            success: function (result) {
                var markup = "<input type='text' id='FamEdit'>";
                $('#div').html(markup);
            },
            complete: function () {
                $('#FamEdit').datepicker({
                    keyboardNavigation: false,
                    autoclose: true,
                    format: "dd M yyyy"
                });
            }
        })

Hi friends. I am binding datepicker to dynamic variable, at this time it troughs an error like datepicker not defined. please help me.. how to bind datepicker in success

DEVASISH BEHARA
  • 115
  • 3
  • 11
  • Do you have multiple elements with the same id of `FamEdit` after the AJAX request completed? Also, please check the console for errors. – Rory McCrossan Sep 15 '17 at 10:19
  • add your html code also – Znaneswar Sep 15 '17 at 10:30
  • Specifically, check the answer below the accepted answer which checks the existence of the datepicker to not overwrite it https://stackoverflow.com/a/34334388/448144 – Nope Sep 15 '17 at 11:19

2 Answers2

-1

Maybe success and complete fired in same time, try to make a timeout like this:

complete: function () {
    setTimeout(function () {
        $('#FamEdit').datepicker({
            keyboardNavigation: false,
            autoclose: true,
            format: "dd M yyyy"
        });
    }, 100);
}

But most likely the best solution is to replace html and initialize the datepicker in one event - success. First replacing, then initialize. Because why do you need to re-initialize the datepicker if no data recived (result in your code)?

xixe
  • 127
  • 1
  • 7
-1

Tested your code on my local page, it work correctly. "datepicker not defined.": Seems that the datepicker.js is not included correctly in your page

VinhNT
  • 1,091
  • 8
  • 13