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/
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.
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.
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.
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!
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/
A simpler solution would be to just add tabindex="-1" to the textbox.
<input type="text" id="myDate" tabindex="-1" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With