4

I have a datepicker input (call it X) on my page already. There's a button that when you click on it, it makes an ajax call and prints some html stuff on page. Inside that ajax response, there's another datepicker input (call it Y), which works when you open it on a standalone page, but inside the ajax call, the Y doesn't show the datepicker box (it works on X however).

The Y already has the "hasDatepicker" class, I've tried to remove it then recall the the datePicker method, but no luck. The only way I've managed to fix it, was to remove the "ui-datepicker-div" from page source and recall the datepicker on Y. But this time, X stops working!

What is wrong with this way of using datepicker? Any way that I can make both of them working correctly?

UPDATE: I've managed to fix this by running these two commands before ajax call and then recalling datepicker after ajax call:

jQuery('.datepicker').datepicker("destroy");
jQuery('#ui-datepicker-div').remove();

I don't know why, but the destroy command does not deletes the ui-datepicker-div and I have to remove it manually! Any clue about this?

2
  • The code is too big to copy it here...I'll try to build an example out of it and post it
    – user926367
    Commented Dec 20, 2014 at 6:15
  • 1
    Thanks a lot @Hossain. You solved my problem too :)
    – sarwar026
    Commented Aug 13, 2017 at 7:03

5 Answers 5

9

For dynamically added datpicker

$('body').on('focus',".datepicker", function(){
    $(this).datepicker();
});​

Demo

6
  • 1
    Running $(this).datepicker(); does not work at all (even using FireBug console)! As I said, I've even removed the hasDatepicker class from it then recalled this method but nothing is happening :-( The demo you gave me is great. I think there's something wrong with my ajax call or something?
    – user926367
    Commented Dec 20, 2014 at 6:22
  • I am giving you working demo example. What your problem happen?
    – Sadikhasan
    Commented Dec 20, 2014 at 6:27
  • I've tested the same thing on a new page. It worked fine. But when I change the ajax request to my page (which is a full contact form), the datepicker on the form won't work. I've removed the datepicker method from the main page and then it worked. Can I dismiss any datepicker on page before making the ajax call?
    – user926367
    Commented Dec 20, 2014 at 6:38
  • You have to write this code on your main page where your ajax data loaded.
    – Sadikhasan
    Commented Dec 20, 2014 at 6:40
  • on focus, yes. I was trying on click earlier for dynamic timepicker, which was requiring field to be selected twice before timelist render. @Sadikhasan Thanks mate!! Can you please explain why focus is only required here rather than click
    – Anant
    Commented Nov 8, 2016 at 15:37
7

I broke my head on this problem for a few days and finally I managed to resolve it.

You just need to remove before reinitialize from DOM some div which have been created by jQuery.

It is:

jQuery('#ui-datepicker-div').remove();

jQuery doesn't reinitialize DatePicker if this div is present in DOM.

My working code here:

jQuery(document).ready(function () {
    InitDatePickers();
});
function RemoveDatePickers() {
    jQuery('#ui-datepicker-div').remove();
}
function InitDatePickers() {
    $('#main .date_inp').datepicker($.datepicker.regional["ru"]);
}
function RefreshData() {
    RemoveDatePickers();
    $.ajax({
        type: 'GET',
        url: "/",
        data: GetFiltersData(),
        success: function (data, textStatus) {
            $("#main").html(data);
            InitDatePickers();
        }

    });
}

Enjoy, guys!

2

My working solution for this issue is remove class hasDatepicker from element with datepicker after ajax call, then reinitialize datepicker

$("#start").removeClass("hasDatepicker");

jQuery("#start").datepicker({ constrainInput: false, dateFormat: "yy-mm-dd", showButtonPanel: true });
1
  • this is the only solution that worked for me, - removing the class before reinit did work, however removing the #ui-datepicker-div did not. weird.
    – user6057915
    Commented Aug 23, 2019 at 14:24
0

Elements added by AJAX have no methods or events bound to them inheritly as they were created AFTER the DOM has loaded. You can work around this with event delegation. However, there's like a better method to what you're trying to do. Instead of adding the datepicker after, why not just have it hidden? If that's not an option, then you need to rebind the datepicker instance in your ajax success.

success: function(data){
    $('body').append(//code to create new_ele);

    //fire off the datepicker again.
    $('new_ele').datepicker();
}
2
  • I had tried to fireup the datepicker method again after the successful ajax call, but no luck! Can you give me an example about the event delegation? Also, how should I make the datepicker hidden (I didn't get your point here, sorry)?
    – user926367
    Commented Dec 20, 2014 at 6:17
  • Have the datepicker exist on the page already at page load. Set it's display to display:none, then toggle it's visibility with jQuery in the ajax success function.
    – Ohgodwhy
    Commented Dec 20, 2014 at 6:19
0

This from jQuery - none of the answers above worked for me:

    $.ajax({url: 'refresh.html', success: function(data) {
          $('#myDiv').html(data). // Load response
                find('input.datepicker').datepicker(); // And reinitialise datepickers
    }});