Created
August 8, 2014 14:20
-
-
Save SethTompkins/9252fc0de9fda59746fd to your computer and use it in GitHub Desktop.
jQuery Datepicker: mm/yyyy format with drop-downs only, also sets the default date to the date the is currently selected. This prevents the date from reverting to today's date when the field is selected after the initial set.
This file contains hidden or 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
| // set a configuration object. This allows you to recursively call it in order to | |
| // change the default date after the initial set. | |
| var datePickerDefaults = { | |
| changeMonth: true, | |
| changeYear: true, | |
| showButtonPanel: true, | |
| dateFormat: 'mm/yy', | |
| onClose: function(dateText, inst) { | |
| // variables to capture the selected month and year. jQuery datepicker acts | |
| // funky without the calndar, so you need to manually grab these values. | |
| var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); | |
| var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); | |
| var date = new Date(year, month, 1); | |
| // add defaultDate to the object, set at what is currently selected. | |
| datePickerDefaults.defaultDate = date; | |
| // recursively set the configuration. | |
| $(this).datepicker('option', datePickerDefaults); | |
| // manually set the date that datepicker returns. | |
| $(this).datepicker('setDate', date); | |
| } | |
| }; | |
| $('.whatever').datepicker( datePickerDefaults ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment