Skip to content

Instantly share code, notes, and snippets.

@tbuyle
Last active January 23, 2020 09:39
Show Gist options
  • Save tbuyle/7b891ee898fc656b002c9db92c741e26 to your computer and use it in GitHub Desktop.
Save tbuyle/7b891ee898fc656b002c9db92c741e26 to your computer and use it in GitHub Desktop.
Set Bootstrap Datepicker to focusDate when tabbing out
# datepicker is accessible trough data("datepicker")
# but its focusDate is cleared out on keydown for tab (9)
# my solution is thus to store the focusDate as data('datepicker_focusdate') on keydown when the key is a navigation key (up, down, left, right)
# and then set the datepicker date to that date when tabbing out
$("body").on("keydown", '[data-provide~=datepicker]', function(e){
if ([37,38,39,40].indexOf(e.keyCode) >= 0 ) {
$(this).data('datepicker_focusdate', $(this).data("datepicker").focusDate)
} else if (e.keyCode == 9 && $(this).data('datepicker_focusdate')) {
$(this).datepicker('update', $(this).data('datepicker_focusdate'));
}
});
@tbuyle
Copy link
Author

tbuyle commented Jun 23, 2016

Updated to avoid setting the field to blank when the first keydown is tab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment