32

I have a datepicker which is used within the jQuery dialog object. The source of the dialog's content is loaded using .load(). Within the dialog I created a script which creates a datepicker for the text input.

$("#date").datepicker({ ... });

When I open the dialog for the first time - everything is okay, but if I close it and reopen again, the datepicker is triggered automatically (and there's no such an option like autoOpen:false) Is there any way of preventing this or what am I doing wrong?

13 Answers 13

33

Much simpler way I found:

$("#dialogPopper").click(
                    function() {
                        $("#date").datepicker("disable");
                        $("#dialog").dialog("open");
                        $("#date").datepicker("enable");
                        return false;
                    }
                  );
2
  • My dialog holds an ajax result so I had to add <disabled: true> in my datepicker options and after <dialog.("open")> I added <datepicker("enable")>. Works like a charm.
    – Palantir
    Commented Jun 12, 2012 at 13:56
  • Don't have idea why, but no any proposed solution worked for me. I have content loaded by AJAX and $(document).on('focus', 'input.datepicker', function () ... - setting 'disable' there and 'enable' when AJAX content loaded and appended to markup, 'disable' again - when dialog is closed. Value now is not changed at all when selecting in calendar - it remained opened.
    – Alexander
    Commented Apr 25, 2016 at 13:42
26

When you put the datepicker field at the beginning of the dialog it is opened automatically. You can place a hidden input at the beginning of the dialog.

<input type="text" style="width: 0; height: 0; top: -100px; position: absolute;"/>
2
  • 1
    this is the only viable solution when the modal is in someone else's plugin unless you want to modify their plugin Commented Mar 8, 2013 at 20:46
  • 2
    Nice ghetto technique. I had some input before, but they were type hidden and it seems it doesn't count. Adding a useless input before works great, but still, add useless dom element and when you post your form, you have this value for nothing.
    – Carlos2W
    Commented Mar 31, 2016 at 19:34
15

I had this exact problem and solved it with only a slight variation on tvanfosson's technique. For some reason I had to manually attach the "click" event to the datepicker field as below.

 $('#dialog').dialog({
 open: function(event, ui) {
    $(ui).find('#date').datepicker().click(function(){
        $(this).datepicker('show');
    });
 },
 close: function(event,ui) {
    $(ui).find('#date').datepicker('destroy');
 }});

(Sorry--I would've preferred to post this as a comment to tvanfosson's post but don't have the requisite rep.)

1
  • 2
    Nice solution, although I had to change ui to this. This is needed when you don't have your dialog ready (in DOM) but build it dynamically with JS.
    – alekwisnia
    Commented Jun 26, 2012 at 13:37
8

You might want to think about destroying the datepicker when the dialog is closed and creating it in the open event handler for the dialog instead of including it as a script in the dialog creation.

 $('#dialog').dialog({
     open: function(event, ui) {
        $(ui).find('#date').datepicker();
     },
     close: function(event,ui) {
        $(ui).find('#date').datepicker('destroy');
     }
 });

You could also experiment with different events/methods to see if you really need to recreate it, but I think that this would work.

2
  • I tried this, and it doesn't work :( May it be because of loading the content using ajax like: $('#dialog').load({ ...}).dialog({ ...}); ?
    – turezky
    Commented May 31, 2009 at 17:59
  • What doesn't work -- is the datepicker not created or does it still appear when reopened? Perhaps you should post the full code.
    – tvanfosson
    Commented May 31, 2009 at 22:00
5

The reason is : your first item inside modal form is the datepicker text field, and when the modal is fired, the active control is the one who contains the datepicker.

I found out two alternative solutions:

  1. Change the order of your fields. Avoid the one with datepicker to stay in first place.

  2. Do not set datepicker to the field in a separate piece of code, do it inside the function that opens the dialog (right after openning $("#dialog").dialog("open");).

4

The reason picker opens by itself, is that the input field stays focused after you open picker for the first time.

You need to blur it:

$input.datepicker({
  onClose: function(dateText) {
    $(this).trigger('blur');
  }
});
1
  • Nice and simple, I called the blur right after the dialog open call and it solved my problem. Commented Apr 24, 2014 at 14:01
4

It's just dialog focus: api.jqueryui.com/dialog/

Upon opening a dialog, focus is automatically moved to the first item that matches the following

  1. The first element within the dialog with the autofocus attribute
  2. The first :tabbable element within the dialog's content
  3. The first :tabbable element within the dialog's buttonpane
  4. The dialog's close button
  5. The dialog itself

A solution is to use the autofocus attribute on other fields than datepicker.

1
  • I like the autofocus option the best. Simple and follows the jquery documentation
    – Kevbo
    Commented Jan 9, 2018 at 18:49
2

I was having a similar problem. I have a jquery datepicker inside a jquery ui dialog. The date picker was opening automatically in IE when I opened the dialog. It was not doing that in Firefox or Chrome... I fixed the problem by disabling the datepicker upon creation in the $(document).ready like so:

$('.ConfirmMove #from').datepicker({
  duration: ''
}).datepicker('disable');

Then when I was opening the dialog containing this datepicker I enabled it in the open event handler of the dialog:

$(".ConfirmMove").dialog({
  close: function() { 
     $('.ConfirmMove #from').datepicker('disable'); 
  },
  open: function() {
     $('.ConfirmMove #from').datepicker('enable');
  }
});

You also have to remember to disable it back when you close the dialog.

This way you also don't destroy and recreate the datepicker each time you open and close the dialog.

1

This is what i did to fix my problem.

This code is in the creation of the dialog.

document.getElementById('date').disabled = true;
setTimeout('document.getElementById("date").disabled = false;', 100);

This way, wen the dialog opens, it will get focus in another control.

You can test the timeout for a smaller amount of delay, but 100 was ok for me.

1

My decision is to combine jsonx's and Pawel Furmaniak's solutions:

<input id='fake-input' type='text' style='width: 0; height: 0; top: -100px; position: absolute;'/>

$( '.datepickerclass' ).datepicker({
   onClose: function() {
$('#fake-input').trigger("focus");
 }
0

I know this is a old question, but one solution that worked for me was triggering off a calendar icon:

$( ".date" ).datepicker({
  showOn: "button",
  buttonImage: "../css/imgs/calendar.gif",
  buttonImageOnly: true
});
0

For some reason the calendar stopped having this behavior when I filled in the animation option in the initializer:

showAnim: Drop

0

From source code I found that jQuery.Dialog always tracks focusin event on elements within dialog, and triggers focus event on that element after dialog gains active state. To prevent this behavior just stop bubbling event propagation from element being focused in.

$("#input").on("focusin", function (e) {
   return false; // or e.stopPropagation();
}).datepicker();

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