jQuery Datepicker reopens itself afer picking a date.(In IE)

562 views Asked by At

On selecting date in datepicker ,its not closing .It keeps on reopening.Helped needed.Thanks.

var $viewStartPicker = $('<input type="text" id="viewStartPicker" />');

$viewStartPicker.datepicker().change(function () {
        var d = new Date(this.value);
        if (d == "Invalid Date")
            this.value = "Invalid Date";
    }); 
2

There are 2 answers

3
DDan On

Which version of IE? Works fine for me in IE 11. fiddle: http://jsfiddle.net/ddan/z4ry6dtu/

<input type="text" id="viewStartPicker" />
$(function() {
    $('#viewStartPicker').datepicker().change(function () {
        var d = new Date(this.value);
        if (d == "Invalid Date")
            this.value = "Invalid Date";
    }); 
});
0
Alexander On

This is the only solution that worked for me. The idea is: second time, when datepicker calendar is being auto-reopened in IE, e.relatedTarget is body (addNewLightBox is class, assigned to jquery dialog, i use datepicker also in other pages, where this dialog is absent - the problem does not exist there):

$(document).on('focus', 'input.datepicker', function (e) {
    var $this = $(this);
    if ($this.closest('.addNewLightBox').length && $(e.relatedTarget).is('body')) {
        $this.datepicker('destroy');
        $this.closest('.addNewLightBox').focus();
        return;
    };
    $this.datepicker({...});
});