8

I want to use a jquery datepicker in a dialog box. The datepicker should be triggered on focus (the default). Since the textbox is the first field on the dialog, it automatically has the focus. This has the unwanted effect of opening the datepicker when the dialog is opened first.

I have tried many different things such as setting the focus to a dummy href, calling datepicker('close') after the dialog opens, setting the showOn to 'button', then changing to 'focus' after the dialog opens but none work.

The datepicker should only be rendered when the textbox gains the focus, except for when the dialog opens first.

My snippet

$(function() {
    $('#btnDialog').click(function() {
        $('#myDate').datepicker({
            title: 'Test Dialog'
        });
        $('#myDialog').dialog();
    });
});​

JS Fiddle link: http://jsfiddle.net/UkTQ8/

0

3 Answers 3

10

Create the datepicker on open and destroy it on close:

$(function() {
    $('#btnDialog').click(function() {
        $('#myDialog').dialog({
            open: function() {
                $('#myDate').datepicker({title:'Test Dialog'}).blur();
            },
            close: function() {
                $('#myDate').datepicker('destroy');
            },
        });
    });
});

DEMO: http://jsfiddle.net/UkTQ8/7/

Sign up to request clarification or add additional context in comments.

1 Comment

Your jsfiddle doens't correspond to your answer code because your answer code is by far the best answer, not your jsfiddle.
10

A simpler solution would be to just add tabindex="-1" to the textbox.

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

1 Comment

By far the simplest and easiest solution +1
6

Solution: Add a dummy text box before the date textbox.

Demo: http://jsfiddle.net/UkTQ8/4/

<div id="myDialog" title="Text Dialog">

<input type="text" id="dummyDate" /> <!-- dummy textbox -->

<p>The textbox below should show the datepicker on focus, except for when the dialog opens.</p>
Enter date: <input type="text" id="myDate" />
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.