Last active
January 23, 2020 09:39
-
-
Save tbuyle/7b891ee898fc656b002c9db92c741e26 to your computer and use it in GitHub Desktop.
Set Bootstrap Datepicker to focusDate when tabbing out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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')); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to avoid setting the field to blank when the first keydown is tab