Skip to content

Commit e667e03

Browse files
authored
gw-preserve-datepicker-date-selection-when-changing-months.js: Added new snippet.
1 parent 6600b88 commit e667e03

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Gravity Wiz // Gravity Forms // Preserve Datepicker Date Selection when Changing Months
3+
* https://gravitywiz.com/
4+
*
5+
* A small quality-of-life improvement that preserves the selected date when changing months via the Datepicker.
6+
* For example, if you selected October 16 and then used the Datepicker's "Month" dropdown to change the month to
7+
* December, the Datepicker would automatically select December 16.
8+
*
9+
* Instructions:
10+
*
11+
* 1. Install this snippet with our free Custom JavaScript plugin.
12+
* https://gravitywiz.com/gravity-forms-custom-javascript/
13+
*/
14+
gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
15+
16+
optionsObj.onChangeMonthYear = function(year, month, inst) {
17+
// Get the currently selected day or default to the 1st if none is selected
18+
var selectedDate = $(this).datepicker('getDate');
19+
var day = selectedDate ? selectedDate.getDate() : 1;
20+
21+
// Set the new date with the updated month and year
22+
$(this).datepicker('setDate', new Date(year, month - 1, day));
23+
}
24+
25+
return optionsObj;
26+
} );

0 commit comments

Comments
 (0)