Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datepicker in a dialog

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/

like image 377
Gwyn Howell Avatar asked Nov 05 '12 13:11

Gwyn Howell


People also ask

How to datepicker in jQuery?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.

How to use datepicker in JavaScript?

We can include datepicker feature in our javascript program by specifying the below inside the <head> and <body> tags. Inside <head> we have to use “datepicker. min. css” and inside <body> tag use “datepicker.

How do you use a date picker?

Date pickers look like text boxes, except that a small calendar icon appears on the right side of the box. To open the pop-up calendar, users click the calendar icon. When the calendar appears, users can click the date that they want on the calendar or use the right and left arrow buttons to scroll through the months.

How to use date picker in html?

The <input type="date"> defines a date picker. The resulting value includes the year, month, and day. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

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/

like image 88
iappwebdev Avatar answered Oct 14 '22 02:10

iappwebdev


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

<input type="text" id="myDate" tabindex="-1" />
like image 23
Narayan Akhade Avatar answered Oct 14 '22 00:10

Narayan Akhade