Created
March 31, 2016 13:37
-
-
Save DavidCramer/ba1722c373bf3772b75237ec43874149 to your computer and use it in GitHub Desktop.
Syncing two date picker fields in Caldera Forms
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
jQuery( function( $ ){ | |
var first_date_id = 'fld_5161421', // date field ID to sync from | |
second_date_id = 'fld_8954452', // date field ID to sync to | |
date_forward = 1; // number of days to sync forward by | |
// get the first field | |
var first_date = $( '[data-field="' + first_date_id + '"]' ); | |
if( !first_date.length ){ | |
return; | |
} | |
// bind date change event | |
first_date.on("changeDate", function() { | |
var start_date = first_date.cfdatepicker('getFormattedDate'); | |
var d = new Date( start_date ); | |
var second_date = first_date.closest('.row').find('[data-field="' + second_date_id + '"]'); | |
d.setDate( d.getDate() + date_forward ); | |
second_date.cfdatepicker( 'setStartDate', new Date(d) ); | |
if( !second_date.val().length ){ | |
second_date.cfdatepicker( 'update', new Date(d) ); | |
} | |
second_date.on("changeDate", function() { | |
second_date.cfdatepicker( 'hide' ); | |
}); | |
first_date.cfdatepicker( 'hide' ); | |
}); | |
}) | |
Dear Desertsnowman, I installed this code on a website of a client and was totally satisfied. It is a booking form, with a minimum stay of 3 days. I set the date_forward to 3. Now my client found, that it is possible to input check in date, after that check out date, and go back to the check in date and change it. Thus avoiding the minimum stay of three days. I think the binding should go in two directions. Can you change this code accordingly?
Regards, Giouris
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Desertsnowman, please can you tell me how I can use this code to sync two date picker fields in caldera forms? Which is the path where I have to save the file in? Thank you in advice