I have an html page that displays a modal dialog with two jquery datepicker fields. When the dialog is instantiated, the cursor is properly placed in the first datepicker field and the calendar is displayed.
Upon selection of a date, focus is moved to the second datepicker field. The calendar flashes but disappears.
How do I correct this?
The code is below. I have added no html formatting, so when the code runs it will be ugly. Pick a date and you will see the date picker calendar, for the to date field, display and disappear.
Any help would be greatly appreciated.
JavaScript
x
70
70
1
<!-- Javascript -->
2
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
3
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
4
5
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
6
<script src="./javascript/jquery-ui.min.js"></script>
7
8
<!-- include needed javascript libraries -->
9
<script defer src="./javascript/jquery.ba-throttle-debounce.min.js"></script>
10
11
<!-- main script -->
12
<script defer>
13
14
$(document).ready(function() {
15
16
// set start and end dates to read only
17
$('#fromdate').prop('readonly', true);
18
$('#todate').prop('readonly', true);
19
20
// display the dialog to enter the date range
21
$("#fromdate").datepicker();
22
$("#todate").datepicker();
23
$("#mtg_dialog").dialog({modal:true, draggable:false, resizable:false});
24
25
// process the selection of the from date
26
$('#fromdate').on('change', function() {
27
if ($('#fromdate').datepicker('getDate') != null) {
28
$('#todate').focus();
29
}
30
});
31
32
// input button
33
$('input[type=button]').hover(function() {
34
$(this).toggleClass('ui-state-hover');
35
});
36
37
// place the cursor in the from date field
38
$("#fromdate").focus();
39
40
/*
41
Run Report Button
42
*/
43
$('#run_btn').on('click', function() {
44
console.log('run report');
45
});
46
47
});
48
49
</script>
50
51
52
<!-- HTML -->
53
<div id="mtg_dialog" style="display:none">
54
<div>
55
<div>
56
<input type="datepicker" placeholder="from date" id="fromdate" tabindex="1"/>
57
<br>
58
<br>
59
<input type="datepicker" placeholder="to date" id="todate" tabindex="2"/>
60
</div>
61
<br>
62
<br>
63
<div>
64
<input type="button" value="Run" id="run_btn" tabindex="3"/>
65
</div>
66
<input type="button" value="Cancel" id="cancel_btn" tabindex="4"/>
67
</div>
68
</div>
69
</div>
70
Advertisement
Answer
you can fix it by delaying the focus event like this:
JavaScript
1
2
1
setTimeout(() => $('#todate').focus());
2