5

I am displaying a jQuery UI dialog window that contains a jQuery UI datepicker control in it. The problem is that the first form field on the dialog window is the datepicker and thus it receives focus when the window opens up which in turn causes the datepicker window to automatically open. I don't want this to occur.

I have tried calling

$('#Date').datepicker('hide')

in then open function of the dialog window and also after the code that makes the dialog open but it doesn't work as when that code is reached, the datepicker window isn't open yet.

How can I make it so that the datepicker window doesn't open when the dialog window shows up but still have it open when the user clicks on the input?

Nick Olsen
  • 6,299
  • 11
  • 53
  • 75
  • possible duplicate of [How to blur the first form input at the dialog opening](http://stackoverflow.com/questions/5418138/how-to-blur-the-first-form-input-at-the-dialog-opening) – Teepeemm Sep 16 '15 at 13:31

5 Answers5

7

You could use the icon trigger: http://jqueryui.com/demos/datepicker/#icon-trigger

And, if needed, prevent the user from typing in a date.

I created a fiddle of a dialog with a datepicker and couldn't duplicate the issue in FF or Chrome. Got it to happen in IE.

http://jsfiddle.net/458bM/2/

$(".datepicker").datepicker({
    dateFormat: 'yy-mm-dd '
});

$(".datepicker").datepicker("disable");

$("#dialog").dialog({
width: 400,
modal: true,
open: function(event, ui) {
    $(".datepicker").datepicker("enable");
    }
});

Based on previous answer: How to blur the first form input at the dialog opening

Community
  • 1
  • 1
jk.
  • 14,365
  • 4
  • 43
  • 58
  • Don't know if that is a jsfiddle issue but if you put it in a normally rendered page it happens in Chrome as well. – Nick Olsen Oct 07 '11 at 17:53
  • @NickOlsen It's the dialog putting focus on the first element. There is a ticket into the jquery ui team to fix. In the meantime, I updated my answer and fiddle. – jk. Oct 07 '11 at 18:25
  • That's somewhat annoying. But, I guess you can't complain to much when it is free! :) – Nick Olsen Oct 07 '11 at 22:46
  • @NickOlsen True, but it looks like the ui team is working on it for version 1.9. – jk. Oct 07 '11 at 23:29
  • 1
    Just adding that you'd want to an the close function to disable it again, if you expect them to open and use the dialog again. – Khaneliman Mar 23 '17 at 01:25
6

You can disable the datepicker when the dialog is not open.

$('#datepicker').datepicker({ 
    ...
    disabled: true 
});

$('#dialog').dialog({
    open: function(event, ui) {
        $('#datepicker').datepicker('enable');
    },
    close: function(event, ui) {
        $('#datepicker').datepicker('disable');
    },
    ...
});

See this in action: http://jsfiddle.net/william/Rf37h/1/.

William Niu
  • 15,798
  • 7
  • 53
  • 93
  • I see it, but when calling from in a separate file, it doen't work. We need to only include jquery1.6.3 ? – Ashok KS Jan 30 '13 at 07:13
0

Try This one line code

$(document).ready (function () { 
 $('#dialog_modal_body').on("click", ".datepicker", function(){
       $("#ui-datepicker-div").css("z-index", "100000");
 });
});
0

Add a hidden element before it that it will try to autofocus to.

This will keep your datepicker from being focused on when opening the dialog.

<span class="ui-helper-hidden-accessible"><input type="text"/>Prevents autofocus from opening start datepicker on dialog load</span>
0

When you define your dialog you can specify the element to receive focus on open. Set the focus to a different element in the dialog

$('#myDialog').dialog({
    open: function (event, ui)
        {
            $('#myInputBox').focus();
        }
});

You could also put your call to hide the datepicker in the open event code.

Geoff
  • 9,340
  • 7
  • 38
  • 48
  • I tried both of these ideas as mentioned in the description and it didn't work. I put a breakpoint on the open function and the datepicker window is not open at that point. It opens after the open function executes. – Nick Olsen Oct 07 '11 at 17:43