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?

1

5 Answers 5

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

5
  • 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
    Commented Oct 7, 2011 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.
    Commented Oct 7, 2011 at 18:25
  • That's somewhat annoying. But, I guess you can't complain to much when it is free! :)
    – Nick Olsen
    Commented Oct 7, 2011 at 22:46
  • @NickOlsen True, but it looks like the ui team is working on it for version 1.9.
    – jk.
    Commented Oct 7, 2011 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
    Commented Mar 23, 2017 at 1: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/.

1
  • 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
    Commented Jan 30, 2013 at 7:13
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.

1
  • 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
    Commented Oct 7, 2011 at 17:43
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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.