Dom
Dom

Reputation: 40459

jQueryUI Dialog/Datepicker Auto-Opens

I currently have a dialog box with two inputs as its content (with the two inputs using .datepicker()). When I open dialog box, the first input becomes the focus and the first datepicker automatically appears. I have tried hiding the div and blurring the input, but that causes the datepicker to no longer appear on focus.

Ideally, I want to be able to the following:

Here is my current code:

JAVASCRIPT:

$("#to").datepicker({
      onClose: function (selectedDate) {
         $("#from").datepicker("option", "minDate", selectedDate);
      }
    });

    $("#from").datepicker({
     onClose: function (selectedDate) {                            
         $("#to").datepicker("option", "maxDate", selectedDate);
    }
});


$("#settingsDialog").dialog({
     autoOpen: false,
    open: function (event, ui) {
                     if ($(".ui-datepicker").is(":visible"))
                         $(".ui-datepicker").hide();

                     $("#to").blur();
                     $this.focus().click();
                 },
     close: function () {
         $(this).dialog("close");
     }
});

I have also made a jsfiddle demo: http://jsfiddle.net/ARnee/19/

I have searched online for a solution, and found similar questions, but none that seem to help. Could anyone assist me?!

EDIT:

The browser I am using is Chrome.

Upvotes: 12

Views: 9755

Answers (4)

kayz1
kayz1

Reputation: 7434

Try setting the tabindex attribute on the inputs containing the datepicker to -1.

<input type="text" id="to" tabindex="-1" />

EDIT:

<input id="to" type="text" tabindex="-1" />
<input id="from" type="text" tabindex="-1" />  

DEMO: http://jsfiddle.net/ARnee/32/

Upvotes: 15

Madi
Madi

Reputation: 146

$(document).ready(function(){



$("#settingsDialog").dialog({
     autoOpen: false,
    open: function (event, ui) {
        $("#to").datepicker({
        onClose: function (selectedDate) {
            $("#from").datepicker("option", "minDate", selectedDate);
        }
    });

    $("#from").datepicker({
        onClose: function (selectedDate) {                            $("#to").datepicker("option", "maxDate", selectedDate);
    }
});
                     if ($(".ui-datepicker").is(":visible"))
                         $(".ui-datepicker").hide();

                     $("#to").blur();
                     $this.focus().click();
                 },
     close: function () {
         $("#to").datepicker( "destroy" );
         $("#from").datepicker( "destroy" );
         $(this).dialog("close");
     }
});



    $("#b1").click(function(){
       $("#settingsDialog").dialog("open");
    });

});
​

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

Can stick a dummy input in dialog that has no height so won't be seen. Place it before the datepicker fields

<input style="height:0px; border:none"/>

DEMO: http://jsfiddle.net/ARnee/20/

Upvotes: 7

mccannf
mccannf

Reputation: 16659

You can create the datepickers upon open of the dialog, like so:

$("#settingsDialog").dialog({
    autoOpen: false,
    open: function (event, ui) {
                     //if ($(".ui-datepicker").is(":visible"))
                     //    $(".ui-datepicker").hide();

                     $("#to").blur();
                 },
     close: function () {
         $("#to").datepicker("destroy");
         $("#from").datepicker("destroy");
         $(this).dialog("close");
     }
});



$("#b1").click(function(){
      $("#settingsDialog").dialog("open");
      $("#to").datepicker({
         onClose: function (selectedDate) {
            $("#from").datepicker("option", "minDate", selectedDate);
         }
      });

      $("#from").datepicker({
         onClose: function (selectedDate) { 
            $("#to").datepicker("option", "maxDate", selectedDate);
         }
      });
});

Upvotes: 1

Related Questions