Is there anyway that i can remain focused on the input element after selecting a date in jQuery datepicker? Also is there anyway that i can prevent user from typing anything but enabling the tab key? I'm currently using prevent default method but i only need to enable the tab key. Thank you. Here is my current method.
$('body').on('focus',".dateSem",function() {
$(this)
.datepicker({
changeMonth: true,
changeYear: true,
changeDay: true,
showButtonPanel: true,
dateFormat: 'yy/MM/dd',
showMonthAfterYear: true,
monthNames: ["01", "02", "03", "04",
"05", "06", "07", "08", "09", "10",
"11", "12"
],
monthNamesShort: ["1", "2", "3", "4",
"5", "6", "7", "8", "9", "10",
"11", "12"
],
dayNamesMin: ["日", "月", "火", "水", "木",
"金", "土"
],
minDate: new Date,
currentText: '今日を選択',
closeText: '確定',
onClose: function(dateText, inst) {
$(this).datepicker(
'setDate',
new Date(inst.selectedYear,
inst.selectedMonth,
inst.selectedDay));
}
});
$(this).keydown(function(e) {
e.preventDefault();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I have made a simple implementation with the following html (please do not forget to include jquery and jqueryui)
<label>Date pick 1</label>
<input type="text" class="mydatepicker" />
<br />
<label>Test focus</label>
<input type="text" /><br />
<label>Date pick 2</label>
<input type="text" class="mydatepicker" />
<br />
<label>Simple text to check tab focus</label>
<input type="text" />
And the javascript code
$('.mydatepicker')
.keydown(function(e){
if(e.keyCode==9){
return true;
}
return false;
})
.datepicker({
onSelect:function(date){
$(this).focus();
}
});
And the explanation
You select all input fields with class .mydatepicker
and assign an onkeydown event where you disable all key input except if the the keycode is equal to 9 (which mean equal to tab)
Then you transform them also datepickers and inside the onSelect function (which runs after the user picks a date) and you call $(this).focus()
where this
is the current input element