I am trying to:
I've written a JQuery script that allows user to select values like 3 Months, 6 Months, 12 Months etc from a dropdown field called Follow Up and selecting it should populate a Datepicker field called Follow Up Date with the correct date based on the selection. That is working and the JQuery code is below. However once a user select one of those, then chooses to use the Datepicker to select another date, the Datepicker field is return random dates.
Link to a page where the issue can be seen:
lien caché
You can select a value from the Follow Up dropdown and it should populate the correct date, which is working. But then try to set another date using the Datepicker and it is returning random dates.
I expected to see:
Even after selecting different date with the datepicker it should set that date correctly.
Instead, I got: I get random dates (past and future)
Here is the JQuery I have set in the Post Form Javascript editor. I leveraged code that I found on a Post on ToolSet Support Forums:
jQuery(window).bind('load', function(){
// put your code inside here
jQuery(".js-cred-follow-up").change(function(event) {
//Only change if Custom Date is not selected.
if (jQuery(".js-cred-follow-up").val() != 9999) {
//console.log("Changed Detected");
var targetDate = new Date();
var to = jQuery('input[name="wpcf-follow-up-date[display-only]"').attr('id');
var to1 = jQuery('input[name="wpcf-follow-up-date[datepicker]"').attr('id');
//console.log("days " + jQuery(".js-cred-follow-up").val());
targetDate.setDate(targetDate.getDate() + parseInt(jQuery(".js-cred-follow-up").val(),10));
//console.log("new date "+ targetDate.getDate());
jQuery( "#"+to).val(targetDate);
//console.log("display only " + jQuery( "#"+to).val());
jQuery(".js-cred-follow-up-date").datepicker("option","dateFormat","MM dd, yy");
jQuery(".js-cred-follow-up-date").datepicker("setDate", targetDate);
//jQuery(".js-cred-follow-up-date").datepicker("option","altFormat","@");
jQuery(".js-cred-follow-up-date").datepicker( "option", "altField","#"+to1);
//console.log(jQuery(".js-cred-follow-up-date").datepicker("option","altField"));
} // if followup is 9999
else {
jQuery(".js-cred-follow-up-date").datepicker("setDate",null);
jQuery( "#"+to).val(null);
jQuery( "#"+to1).val(null);
jQuery(".js-cred-follow-up-date").datepicker( "option", "altField",null);
console.log(jQuery(".js-cred-follow-up-date").datepicker("getDate"));
console.log("display only " + jQuery( "#"+to).val());
jQuery(".js-cred-follow-up-date").datepicker( "refresh" );
}
});
});